DEV Community

DCT Technology Pvt. Ltd.
DCT Technology Pvt. Ltd.

Posted on

2 1 1 1 1

Props vs State in React: Are You Using Them Right? ๐Ÿค”

Ever felt like React is playing mind games with you? Props and State can seem like twins, but they serve very different purposes โ€” and mixing them up can cause chaos in your components. ๐Ÿš€

Image description

Letโ€™s break it down so you never get confused again! ๐Ÿ‘‡

๐Ÿง  Props: The Data You Pass Down

Think of props like care packages you send to components. They are:

  1. Read-only (immutable)
  2. Passed from parent to child
  3. Great for static or external data

๐Ÿ”น Example:


function Greeting(props) { 
  return <h1>Hello, {props.name}!</h1>; 
} 
Enter fullscreen mode Exit fullscreen mode

Here, name is a prop โ€” and the component canโ€™t change it.

๐Ÿ”‘ Tip: Use props for anything that shouldnโ€™t change within a component (like config values or content).

๐ŸŸข State: The Data You Control

State is like the inner mood of a component โ€” it lives inside and can change over time. Itโ€™s perfect for:

  1. Dynamic data (like form inputs)

  2. User interactions

  3. Component lifecycle changes

๐Ÿ”น Example:


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

  return ( 
    <div> 
      <p>Count: {count}</p> 
      <button onClick={() => setCount(count + 1)}>Increment</button> 
    </div> 
  ); 
} 
Enter fullscreen mode Exit fullscreen mode

Here, count is state โ€” and the component can change it with setCount.

โšก Key Differences (In Simple Terms)

Props = Data IN (from parent) ๐Ÿ“จ

State = Data OUT (componentโ€™s private data) ๐Ÿ“ค

Props are fixed unless the parent component changes them.

State is flexible and can change within the component itself.

๐Ÿšจ Common Mistake: Donโ€™t try to modify props inside a component. If you need to change data, lift the state up and pass it back down as a prop!

๐Ÿ“Œ Quick Analogy:

Imagine a vending machine ๐Ÿฅค

Props = The snacks inside the machine (supplied by the owner)

State = The current stock count of each snack (tracked by the machine itself)

If you buy a snack, state updates, but the props stay the same until the owner refills the machine.

๐Ÿ’ฌ Are you handling props and state like a pro, or have you had any "aha!" moments while learning React?

Let me know in the comments! ๐Ÿš€

๐Ÿ“Œ Follow DCT Technology for more hands-on web development tips and tricks!

ReactJS #WebDevelopment #FrontendDevelopment #JavaScript #CodingTips #ReactHooks #Programming #TechInsights #DCTTechnology

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
pengeszikra profile image
Peter Vivo โ€ข โ€ข Edited

Props is give a pure component solution because we can avoide the inner state.
Props working as state, to modify we need to use that state set method.

const FooComp = ({id, title, post, linkInfo, linkAddress, setLink}) => (
  <main>
    <h1>{title}</h1>
    <p>{post}</p>
    <button onClick={() => setLink(linkAddress)}>{linkInfo}</button>
  </main>
);
Enter fullscreen mode Exit fullscreen mode

That why I write a npm module: react type safe useReducer to easy make a typesafe useReducer solution which is work:

const ParentComponentWIthComplexState = () => {
  const [state, actions] = useDuck(reducer, initialState, actionsLabels);

  return (
    <section>
       <Panel state={state} actions={actions} />
       <OtherPanel state={state} actions={actions} />
    </section>
  )
}
Enter fullscreen mode Exit fullscreen mode

Panel and OtherPanel can use any state, and actions to handle our ParentComponent
Technically we can pass whole actions to safe because that is don't change on run. A lower component level we can select which state pass it to it.

But I am moving fare away from React, but this simple solution can save my days a lot. The problem is writing a good reducer is takes a times.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

๐Ÿ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someoneโ€™s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay