Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

React Props vs State: What Every Developer Should Know

Posted on April 21, 2025 • 7 min read • 1,379 words
Share via
Mapagam
Link copied to clipboard

Learn the key differences between React props and state, their use cases, best practices, and how to manage dynamic data in your apps.

On this page
1. Introduction to Props and State 1.1. The Basics of React’s Data Flow 1.2. Why Understanding Props and State Matters 2. React Props: What Are They and When to Use Them 2.1. Understanding Props 2.2. When to Use Props 2.3. Pitfalls of Using Props 2.4. Best Practices for Using Props 3. React State: What Is It and When to Use It 3.1. Understanding State 3.2. When to Use State 3.3. Pitfalls of Using State 3.4. Best Practices for Using State 4. Comparing Props and State 4.1. Key Differences 4.2. React’s Data Flow 5. Real-World Example: Props and State in Action 5.1. Scenario: A Task List Application 6. Common Mistakes to Avoid 6.1. Overuse of State 6.2. Directly Mutating Props 6.3. Not Using the Proper State Initialization 7. Conclusion 7.1. Key Takeaways 7.2. Final Thoughts

When you’re building React applications, one of the most important concepts you’ll encounter is props and state. Both are used to manage data in a React app, but understanding their differences, use cases, and how they work together is crucial to writing clean, effective code. This article will break down React Props vs State, explain their roles in building dynamic and interactive UIs, and provide practical insights for developers of all skill levels.

1. Introduction to Props and State

1.1. The Basics of React’s Data Flow

In React, props and state are two fundamental concepts that govern how data flows and changes within components. Props are short for “properties” and are used to pass data from a parent component to a child component. State, on the other hand, represents the local data that a component manages and can change over time.

When used properly, props and state help in creating a dynamic, responsive application that reacts to user input and other changes in the environment.

1.2. Why Understanding Props and State Matters

Understanding the React data flow is vital for developers working on everything from small applications to large-scale, complex React projects. Knowing when and how to use props and state correctly leads to:

  • Better performance: React can efficiently re-render components when state or props change.
  • Clean code: Proper separation of concerns between state and props leads to maintainable code.
  • Predictable behavior: Understanding data flow helps in creating applications where behavior is easy to predict and debug.

In this article, we’ll explore props and state, when to use them, and how they interact.

2. React Props: What Are They and When to Use Them

2.1. Understanding Props

Props are the inputs to a React component, passed down from a parent component. They allow for customization of child components without directly modifying them. Props are immutable, meaning they cannot be changed inside the component that receives them.

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

function App() {
  return <Greeting name="Alice" />;
}

In the example above, name is a prop passed from the App component to the Greeting component. It’s important to note that props should be treated as read-only by the child components.

2.2. When to Use Props

Props are typically used when:

  • Passing static data from parent to child components (e.g., configuration values, display data).
  • Sharing callback functions for event handling or interaction between components.

Props are also useful when components need to share state indirectly or when you want to re-render a child component whenever the prop changes.

2.3. Pitfalls of Using Props

A common pitfall when using props is over-relying on them to manage dynamic or interactive data. For example, if you use props to pass mutable data that needs to be updated, your application may become hard to manage. A better approach is to use state in such cases.

2.4. Best Practices for Using Props

  • Keep props simple: Avoid complex logic in props. Props should primarily be used for data transfer, not for logic.
  • Use default props: In some cases, it’s useful to set default values for props when they are not passed.
Greeting.defaultProps = {
  name: "Guest",
};

3. React State: What Is It and When to Use It

3.1. Understanding State

State represents the local data of a component. It can be changed by the component itself through user interactions, lifecycle events, or other triggers. Unlike props, which are passed down from a parent, state is managed within the component.

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increase</button>
    </div>
  );
}

In this example, the Counter component has a piece of state called count, which starts at 0 and can be updated by clicking the button.

3.2. When to Use State

State is used when:

  • Data needs to be mutable or can change over time (e.g., form inputs, counters, toggles).
  • User interactions or external events need to trigger changes in the component.
  • You need to manage UI changes that are specific to the component, like showing or hiding elements, etc.

3.3. Pitfalls of Using State

One potential issue when using state is managing too much state. For example, storing too many variables in state can lead to unnecessary re-renders and can make your code harder to maintain. It’s also important not to mutate state directly but rather use React’s setState or useState to ensure state changes are handled properly.

// Bad: Directly modifying state
this.state.count = 5;

// Good: Using setState
this.setState({ count: 5 });

3.4. Best Practices for Using State

  • Encapsulate related state: Group related pieces of state together to keep things organized.
  • Minimize unnecessary re-renders: Use memoization techniques like React.memo or useMemo to prevent re-renders when props or state haven’t changed.
  • Lift state up: When multiple components need to share the same state, it’s often better to lift the state up to the nearest common ancestor.
function ParentComponent() {
  const [sharedState, setSharedState] = useState(false);

  return (
    <div>
      <ChildComponent sharedState={sharedState} setSharedState={setSharedState} />
    </div>
  );
}

4. Comparing Props and State

4.1. Key Differences

Feature Props State
Definition Data passed from parent to child. Local data managed by the component.
Mutability Immutable within the child component. Mutable; can change over time.
Source Set by the parent component. Set and managed within the component.
Usage For passing data and event handlers. For managing data that changes over time.
Reactivity Trigger re-renders when props change. Trigger re-renders when state changes.

4.2. React’s Data Flow

  • Props flow downward from parent to child.
  • State flows upward when shared between components (via callbacks or lifting state).

In a typical React app, you’ll often see a combination of both props and state, where props are used to pass static data, and state is used to manage dynamic interactions.

5. Real-World Example: Props and State in Action

Let’s look at a practical example where we use both props and state together.

5.1. Scenario: A Task List Application

Imagine you’re building a task list application where users can add tasks, mark them as complete, and delete them.

function TaskList({ tasks }) {
  const [completedTasks, setCompletedTasks] = useState([]);

  const toggleTaskCompletion = (taskId) => {
    setCompletedTasks((prevCompleted) =>
      prevCompleted.includes(taskId)
        ? prevCompleted.filter((id) => id !== taskId)
        : [...prevCompleted, taskId]
    );
  };

  return (
    <ul>
      {tasks.map((task) => (
        <li
          key={task.id}
          style={{ textDecoration: completedTasks.includes(task.id) ? "line-through" : "none" }}
        >
          <span>{task.name}</span>
          <button onClick={() => toggleTaskCompletion(task.id)}>
            {completedTasks.includes(task.id) ? "Undo" : "Complete"}
          </button>
        </li>
      ))}
    </ul>
  );
}

function App() {
  const tasks = [
    { id: 1, name: "Do the dishes" },
    { id: 2, name: "Take out the trash" },
  ];

  return <TaskList tasks={tasks} />;
}

In this example:

  • The App component passes the tasks array as a prop to the TaskList component.
  • The TaskList component manages the list of completed tasks using state, and updates it when a task is toggled.

6. Common Mistakes to Avoid

6.1. Overuse of State

State is essential, but overusing it can make your app more complex than necessary. Be selective about what you store in state, and prefer props when data doesn’t need to change.

6.2. Directly Mutating Props

Never mutate props directly. Props are immutable within a component, so always treat them as read-only. Instead, use callbacks passed through props to request changes in the parent component.

6.3. Not Using the Proper State Initialization

When using hooks like useState, ensure that you initialize state correctly. For example, if you are managing an array or object, you should initialize it as an empty array or object to avoid errors.

const [items, setItems] = useState([]); // Correct initialization

7. Conclusion

7.1. Key Takeaways

  • Props are used for passing data and event handlers from parent to child components.
  • State is used for managing local data that can change over time.
  • Props are immutable, while state is mutable and can trigger component re-renders.
  • Keep props simple and pass data from parent to child. Use state for interactive and dynamic data that needs to change over time.
  • Use lifting state up to share state across components.

7.2. Final Thoughts

React’s separation of props and state is one of the core principles that makes it a powerful library for building dynamic user interfaces. Understanding how and when to use each will make you a better React developer and help you build more scalable, maintainable applications.

React Props   React State   React Development   JavaScript State Management   React Best Practices  
React Props   React State   React Development   JavaScript State Management   React Best Practices  
Understanding useState in React: A Beginner-Friendly Guide 

More Reading!

  1. JSX in React: The Syntax Behind the Magic (With Real-World Examples)
  2. How React’s Virtual DOM Works (Explained Visually for Developers)
  3. React State vs. Props: Understanding the Key Differences
  4. React Lists and Keys Explained (Why You Should Use Them)
  5. React Hooks vs. Class Components: Which One Should You Use?
On this page:
1. Introduction to Props and State 1.1. The Basics of React’s Data Flow 1.2. Why Understanding Props and State Matters 2. React Props: What Are They and When to Use Them 2.1. Understanding Props 2.2. When to Use Props 2.3. Pitfalls of Using Props 2.4. Best Practices for Using Props 3. React State: What Is It and When to Use It 3.1. Understanding State 3.2. When to Use State 3.3. Pitfalls of Using State 3.4. Best Practices for Using State 4. Comparing Props and State 4.1. Key Differences 4.2. React’s Data Flow 5. Real-World Example: Props and State in Action 5.1. Scenario: A Task List Application 6. Common Mistakes to Avoid 6.1. Overuse of State 6.2. Directly Mutating Props 6.3. Not Using the Proper State Initialization 7. Conclusion 7.1. Key Takeaways 7.2. Final Thoughts
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard