In React, 'Props' (short for properties) and 'State' are fundamental concepts for managing data within your applications. They dictate how components communicate and how dynamic data changes over time, leading to re-renders.
1. Props (Properties)
* Definition: Props are arguments passed into React components. They are read-only attributes that allow you to pass data from a parent component to a child component.
* Purpose: Props are used for configuring child components, passing data down the component tree, and enabling communication from parent to child.
* Characteristics:
* Immutable: Within the child component, props cannot be changed. A child component should never modify the props it receives from its parent.
* One-way data flow: Data always flows down the component tree, from parent to child. A child component cannot directly pass props back up to its parent.
* External Data: Props essentially provide external data to a component, allowing it to be reusable and configurable.
* Analogy: Think of props as the arguments you pass to a function. The function uses these arguments to perform its task but doesn't (and shouldn't) change the original arguments themselves.
2. State
* Definition: State is an object that holds data internal to a component that can change over time. When the state changes, the component re-renders.
* Purpose: State is used to manage dynamic data that belongs to a component and can be altered by user interactions, network responses, or other events within the component itself.
* Characteristics:
* Mutable: Unlike props, state is designed to be changed. However, it should only be updated using the `setState()` method (for class components) or the `useState` hook (for functional components).
* Internal Data: State represents the component's internal memory or data that it manages itself.
* Causes Re-render: When a component's state changes, React efficiently re-renders that component and its children to reflect the updated data.
* Analogy: Think of state as a component's internal memory or private data. It can read and update this data itself, and these updates will cause the component to show a new 'view' of itself.
Relationship and Distinction
* Props are for communication BETWEEN components (parent to child). They make components reusable and allow data to flow downwards.
* State is for managing data WITHIN a component. It allows components to be dynamic and interactive.
* A component's state can be passed down as props to its child components. This is a common pattern: a parent component holds state, and passes parts of that state (or functions to update that state) as props to its children.
In functional components, the `useState` hook is the primary way to manage state. It returns a pair: the current state value and a function to update it.
Example Code
```jsx
import React, { useState } from 'react';
// Child Component: Receives 'count' as a prop and displays it.
function DisplayCounter(props) {
return (
<div style={{ padding: '10px', border: '1px solid lightblue', borderRadius: '5px', margin: '10px' }}>
<h3>Child Component (DisplayCounter)</h3>
<p>Current Count (received via props): <strong>{props.count}</strong></p>
{/* props.count = 10; // This would cause an error or warning, as props are read-only */}
</div>
);
}
// Parent Component: Manages its own 'count' state and passes it as a prop to DisplayCounter.
function App() {
// Declare a state variable 'count' and a function 'setCount' to update it.
// Initial value of count is 0.
const [count, setCount] = useState(0);
// Function to increment the count state
const incrementCount = () => {
setCount(prevCount => prevCount + 1); // Use functional update for safety
};
// Function to decrement the count state
const decrementCount = () => {
setCount(prevCount => prevCount - 1);
};
return (
<div style={{ fontFamily: 'Arial, sans-serif', textAlign: 'center' }}>
<h1>Parent Component (App)</h1>
<p>App's Internal Count State: <strong>{count}</strong></p>
<button onClick={incrementCount} style={{ margin: '5px', padding: '10px', fontSize: '16px' }}>
Increment Count
</button>
<button onClick={decrementCount} style={{ margin: '5px', padding: '10px', fontSize: '16px' }}>
Decrement Count
</button>
<hr style={{ margin: '20px 0' }}/>
{/* Pass the 'count' state from App as a prop named 'count' to DisplayCounter */}
<DisplayCounter count={count} />
</div>
);
}
export default App;
```








Props and State Management in React