React Props vs State: Complete Beginner's Guide

Understanding Props and State in React: A Beginner's Guide#
1. Introduction#
React is a JavaScript library used for building user interfaces, especially single-page applications. One of the main reasons React is so popular is because of its component-based architecture and declarative nature.
In React, the UI is broken down into small, self-contained pieces called components. Each component manages its own structure, logic, and styling, which makes it easier to build complex user interfaces by composing them from simpler building blocks.
On top of that, React's declarative approach means you describe what you want the UI to look like for a given state, rather than writing step-by-step instructions for how to update it. This leads to more predictable and easier-to-maintain code, especially as applications grow in size and complexity.
This allows developers to create reusable, dynamic components, and React takes care of updating the user interface efficiently when data changes.
However, there are two fundamental concepts in React that every developer must understand: props and state. While these concepts may seem similar at first, they play different roles in the behavior of React applications. Understanding the differences between props and state is crucial for writing clean, maintainable, and powerful React components.
In this article, we will cover what props and state are, when and why to use them, and how they work together to manage data in a React application.
2. What Are Props?#
2.1 Definition of Props#
In React, props are short for properties. They are used to pass data from one component to another, typically from a parent component to a child component. This makes components more dynamic and reusable.
You can think of props like parameters you pass to a function. Just as you pass values to a function in JavaScript, you pass props to components in React.
hljs jsxfunction Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
In the example above, Greeting is a function component that accepts props. It uses the name prop to display a personalized greeting.
2.2 How to Use Props#
Props are passed to components similarly to how attributes are passed to HTML elements:
hljs jsxfunction App() {
return <Greeting name="Alice" />;
}
In this case, we are passing a prop called name with the value "Alice" to the Greeting component. Inside the Greeting component, we can access props.name and display the message "Hello, Alice!"
2.3 Example: Passing Data with Props#
Here's a more practical example where a parent component passes multiple pieces of data as props to a child component:
hljs jsxfunction ProfileCard(props) {
return (
<div className="profile-card">
<h2>{props.username}</h2>
<p>Age: {props.age}</p>
<p>Location: {props.location}</p>
</div>
);
}
function App() {
return (
<ProfileCard username="JohnDoe" age={28} location="New York" />
);
}
Here, the ProfileCard component receives three props, username, age, and location. The App component acts as the parent and passes these props to ProfileCard.
2.4 Reusability with Props#
One of the best things about props is that they make your components reusable. Since you can pass different data through props, the same component can display different information depending on the props it receives.
For example, you could create multiple profile cards with different user data:
hljs jsxfunction App() {
return (
<div>
<ProfileCard username="JohnDoe" age={28} location="New York" />
<ProfileCard username="JaneSmith" age={34} location="London" />
<ProfileCard username="AlexW" age={21} location="Sydney" />
</div>
);
}
3. Real-World Examples of Props#
Now that we know how props work in theory, let's look at some real-world scenarios:
- User Profiles: Passing user data like name, profile picture, and bio from a parent component to a profile card child component.
- Buttons: Passing a label and a click handler to a custom button component.
- E-commerce Product Cards: Passing product details like price, name, and image to a product card component.
Props allow components to be flexible by accepting data from a parent component, while keeping the logic for displaying that data inside the component itself.
4. What Is State?#
4.1 Definition of State#
While props allow data to be passed down from parent to child, state is used to store and manage data within a component itself. State represents the data or properties that can change over time in response to user input, network responses, or other factors.
Unlike props, state is mutable, it can be changed. Every time state changes, React re-renders the component to reflect the updated state in the UI.
4.2 How to Use State#
To use state in a functional component, we use the useState hook.
Hooks are special functions in React that let functional components "hook into" features like state and lifecycle behavior that were previously only available in class components.
This hook allows us to declare a state variable and a function to update it.
hljs jsximport React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increase Count</button>
</div>
);
}
In this example, count is a state variable initialized to 0. When the button is clicked, the setCount function updates the state to increment the count.
4.3 Example: Using State#
Here's another practical example. Imagine a toggle button that changes its label between "On" and "Off" every time you click it:
hljs jsximport React, { useState } from 'react';
function ToggleButton() {
const [isOn, setIsOn] = useState(false);
return (
<button onClick={() => setIsOn(!isOn)}>
{isOn ? 'Turn Off' : 'Turn On'}
</button>
);
}
In this example, isOn is a boolean state variable. When the button is clicked, the state toggles between true and false, updating the button's label accordingly.
5. The Key Differences Between Props and State#
Let's summarize the main differences between props and state:
| Props | State |
|---|---|
| Passed from parent to child | Managed within the component |
| Immutable (cannot be changed) | Mutable (can be updated) |
| Used for static, read-only data | Used for dynamic, changing data |
| Controlled by the parent component | Controlled by the component itself |
5.1 Props vs. State in Practical Scenarios#
- Use Props when you want to pass data from a parent component to a child component that doesn't change.
- Use State when the data is managed within the component and can change over time (like form inputs, toggle switches, counters, etc.).
6. Combining Props and State#
6.1 Prop Drilling#
Prop drilling happens when data is passed down through several layers of components. For example, if you need to pass a piece of state from a parent component to a deeply nested child component, you might need to pass it through multiple intermediate components as props.
6.2 Using Props and State Together#
Props and state often work together. For example, you might pass a function as a prop to a child component so that the child can trigger a state update in the parent:
hljs jsxfunction App() {
const [message, setMessage] = useState('');
return (
<div>
<MessageInput setMessage={setMessage} />
<p>Message: {message}</p>
</div>
);
}
function MessageInput({ setMessage }) {
return (
<input
type="text"
onChange={event => setMessage(event.target.value)}
/>
);
}
Here, the MessageInput component is using a prop (setMessage) to update the state in the parent component (App).
7. Advanced Props and State Concepts#
7.1 Lifting State Up#
Sometimes, two components may need to share the same piece of state. In this case, it's common to lift the state up to the nearest common ancestor component and pass it down as props.
7.2 Controlled vs. Uncontrolled Components#
In React forms, controlled components are those where the form input elements' values are controlled by React state. This means that the form's state is tied to the component's state, allowing React to manage and control it.
hljs jsxfunction TextInput() {
const [text, setText] = useState('');
return (
<input
value={text}
onChange={event => setText(event.target.value)}
/>
);
}
In contrast, uncontrolled components use the DOM to manage the form's state.
8. Real-World Examples and Use Cases#
8.1 Props in Action#
- E-commerce: Passing product data (name, price, description) to a product card component.
8.2 State in Action#
- Form handling: Tracking form input values with state in order to manage user input dynamically.
8.3 Mixing Props and State in Real Life#
- Search Functionality: Pass a search term (state) to a search results component as props to filter data dynamically.
9. Conclusion#
Props and state are essential building blocks of any React application. Props allow you to pass data between components, making them more reusable and dynamic. State, on the other hand, allows components to manage and react to data that changes over time. Understanding when and how to use these two tools is key to building powerful, efficient, and maintainable applications in React.
Mastering the distinction between props and state will enable you to write better React components and unlock the full potential of this powerful library.
10. Further Reading and Resources#
- Official React documentation on Props.
- Official React documentation on State.
- Tutorials on mastering props, state, and hooks in React.