State and props are fundamental concepts in React that enable developers to create dynamic, interactive user interfaces. Understanding how they work and differ is crucial for mastering React development.
What Are Props?
Props (short for properties) are used to pass data from one component to another, typically from a parent component to a child component. Props are read-only and cannot be modified by the receiving component.
Example:
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
function App() {
return <Greeting name="Alice" />;
}
In this example, the name
prop is passed from the App
component to the Greeting
component.
Key Features of Props:
- Immutable: Props cannot be changed by the child component.
- Passed from parent to child.
- Help create reusable components by making them configurable.
What Is State?
State is a built-in React object that is used to manage dynamic data within a component. Unlike props, state is mutable and can be updated using the setState
method in class components or the useState
Hook in functional components.
Example:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
Here, count
is a state variable that is updated when the button is clicked.
Key Features of State:
- Mutable: State can be changed using functions like
setState
oruseState
. - Local: State is managed within the component.
- Triggers re-renders when updated.
Props vs. State: A Comparison
Feature | Props | State |
---|---|---|
Mutability | Immutable | Mutable |
Source | Passed from parent to child | Managed withina the component |
Purpose | Share data between components | Handle dynamic, interactive data |
Updates | Cannot be updated by the receiving component | Can be updated using setState or useState |
How Props and State Work Together
In many React applications, props and state are used in tandem. Props can be passed to a child component, and state can determine how the component behaves or what it displays.
Example:
function ItemList({ items }) {
return (
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}
function App() {
const [items, setItems] = useState(["Item 1", "Item 2", "Item 3"]);
return (
<div>
<h1>My Items</h1>
<ItemList items={items} />
</div>
);
}
Here, the items
state in the App
component is passed as a prop to the ItemList
component.
Best Practices for Using Props and State
- Keep State Localized: Only manage state in components that need it.
- Use Props for Configuration: Pass data and event handlers via props to make components reusable.
- Avoid Overusing State: Too many state variables can make components difficult to manage. Use context or state management libraries for complex applications.
Final Thoughts
Props and state are the backbone of React’s component-driven architecture. By understanding their roles and interplay, you can build dynamic and maintainable applications. Mastering these concepts is the first step toward becoming a proficient React developer.
About Lavesh Katariya
Innovative Full-Stack Developer | Technical Team Lead | Cloud Solutions Architect
With over a decade of experience in building and leading cutting-edge web application projects, I specialize in developing scalable, high-performance platforms that drive business growth. My expertise spans both front-end and back-end development, making me a versatile and hands-on leader capable of delivering end-to-end solutions.