React Unit 3 Sol
React Unit 3 Sol
**React Props:**
Props (short for properties) are read-only data that are passed from
a parent component to a child component. They allow parent
components to communicate with their children by passing data or
behavior down the component tree. Props are immutable and
cannot be modified by the child component.
```jsx
// ParentComponent.js
import React from 'react';
import ChildComponent from './ChildComponent';
function ParentComponent() {
return (
<div>
<ChildComponent message="Hello, child!" />
</div>
);
}
```jsx
// ChildComponent.js
import React from 'react';
function ChildComponent(props) {
return (
<div>
<p>{props.message}</p>
</div>
);
}
export default ChildComponent;
```
**React State:**
```jsx
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
**Summary:**
**Props:**
Props (short for properties) are a way to pass data from parent
components to child components in React. They are immutable and
read-only within the child component, meaning that the child
component cannot modify the props it receives from its parent.
**When to use props:**
**State:**
```jsx
import React, { useState } from 'react';
function MyComponent() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
```
```jsx
import React, { Component } from 'react';
increment = () => {
this.setState({ count: this.state.count + 1 }); // Updates the count
state
};
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.increment}>Increment</button>
</div>
);
}
}
```
**Why Updating State is Important in a Dynamic Application:**
**Functional Components:**
```jsx
import React from 'react';
function FunctionalComponent(props) {
return (
<div>
<h1>Hello, {props.name}!</h1>
</div>
);
}
**Class Components:**
```jsx
import React, { Component } from 'react';
incrementCount = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
<div>
<h1>Count: {this.state.count}</h1>
<button onClick={this.incrementCount}>Increment</button>
</div>
);
}
}
**Summary:**
**Example:**
Parent Component:
```jsx
import React from 'react';
import ChildComponent from './ChildComponent';
function ParentComponent() {
const data = "Hello from Parent";
return (
<div>
<ChildComponent message={data} />
</div>
);
}
Child Component:
```jsx
import React from 'react';
function ChildComponent(props) {
return (
<div>
<p>Message from Parent: {props.message}</p>
</div>
);
}
**Card Component:**
```jsx
import React from 'react';
```jsx
import React from 'react';
import Card from './Card';
function CardList() {
const cardsData = [
{ id: 1, title: "Card 1", content: "Content for card 1" },
{ id: 2, title: "Card 2", content: "Content for card 2" },
{ id: 3, title: "Card 3", content: "Content for card 3" }
];
return (
<div className="card-list">
{cardsData.map(card => (
<Card key={card.id} title={card.title} content={card.content} />
))}
</div>
);
}
**Usage:**
In your main component (or App component), you can use the
`CardList` component like this:
```jsx
import React from 'react';
import CardList from './CardList';
function App() {
return (
<div className="app">
<h1>Card List Example</h1>
<CardList />
</div>
);
}
This code will render a list of cards with titles and content specified
in the `cardsData` array in the `CardList` component. Each card is
rendered using the `Card` component, which receives the title and
content as props.