6. React js State Vs Props where to use what

6. React js State Vs Props where to use what

AI Reading

Quick summary of this article

React components use two main ways to handle data: props and state. Props are read-only data passed from parent to child components, while state is mutable data managed within a component itself. Understanding when to use each is essential for building React applications.

  • Props (properties) are passed from parent components to child components and cannot be changed by the receiving component.
  • State is a built-in React object that is local to a component and can be updated over time using functions like useState.
  • Use props to pass data and configuration down to child components; use state to track and update data that changes within a component.
  • Props are received as function parameters, while state is managed using hooks like useState and can trigger re-renders when updated.
  • Components often combine both: receiving data via props and managing internal changes with state, as shown in the example where a User component receives a name prop and tracks likes with state.

📝 React Blog: Understanding State and Props with Examples

🔹 What are Props in React?

Props (short for properties) are used to pass data from parent components to child components. They are read-only, meaning a component cannot change the props it receives.

✅ Props Example


const App = () => {
  return (
    <div>
      <h1>Props Example</h1>
      <Greeting name="Anjum" />
    </div>
  );
};

const Greeting = ({ name }) => {
  return <h2>Hello, {name}!</h2>;
};

Explanation:
The App component passes name="Anjum" to Greeting.
Greeting receives name via props and displays “Hello, Anjum!”.


🔹 What is State in React?

State is a built-in object that allows components to track changes over time. Unlike props, state is mutable and local to the component.

✅ State Example


import React, { useState } from 'react';

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

  return (
    <div>
      <h1>State Example</h1>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click Me
      </button>
    </div>
  );
};

export default Counter;

Explanation:
useState(0) initializes count to 0.
When the button is clicked, setCount updates the state, re-rendering the component.


🔁 Props + State Combined Example


const App = () => {
  return (
    <div>
      <h1>Combined Example</h1>
      <User name="Aisha" />
    </div>
  );
};

const User = ({ name }) => {
  const [likes, setLikes] = React.useState(0);

  return (
    <div>
      <h2>Hello, {name}</h2>
      <p>{name} has {likes} likes</p>
      <button onClick={() => setLikes(likes + 1)}>
        Like
      </button>
    </div>
  );
};

Takeaways:
App passes name="Aisha" via props.
User uses useState to track likes. Each click increases likes for Aisha.


🧠 Conclusion

Props State
Passed from parent Local to component
Read-only Mutable
Used for communication Used for internal changes
Received via function parameters Managed via useState()

Leave a Reply

Your email address will not be published. Required fields are marked *