Best Practices for Props and State
0 108
โ Best Practices for Props and State in React
In React, props and state are the two main tools used to control dynamic behavior and flow of data. They help you build reusable, modular, and interactive components. But misusing them can make your code messy, buggy, or hard to maintain.
In this blog, weโll go through essential best practices for handling props and state in your React components so you can keep your apps clean, scalable, and efficient. ๐ก
๐ฅ Keep Props Read-Only
Props are meant to be passed down from parent to child. They should never be modified inside the child component. Instead, use callbacks if the child needs to "inform" the parent about something.
// โ Never do this
props.username = "NewUser";
// โ
Do this instead
const Greeting = ({ username }) => (
<h2>Hello, {username}!</h2>
);
๐จ Tip: Always treat props as immutable!
๐ Use State for Interactivity
State is for data that changes over time within a component โ form inputs, toggles, counters, etc.
const Counter = () => {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
};
Only use state for things that will trigger a UI update when changed.
๐ Keep State Minimal
Don't store derived or unnecessary data in state. If something can be calculated from props or existing state, you donโt need to store it separately.
// โ Redundant state
const [fullName, setFullName] = useState(firstName + " " + lastName);
// โ
Derive when needed
const fullName = firstName + " " + lastName;
This avoids synchronization issues and reduces bugs.
๐งฉ Lift State When Necessary
When two sibling components need the same data, move the state to their closest common ancestor and pass it down as props.
const Parent = () => {
const [text, setText] = useState("");
return (
<>
<Input value={text} onChange={setText} />
<Preview value={text} />
</>
);
};
This is known as lifting state up โ it keeps your data in sync and your logic centralized.
๐ Use Callbacks to Modify Parent State
If a child component needs to trigger a change in the parentโs state, pass down a callback from the parent.
const Parent = () => {
const [name, setName] = useState("");
return <Child onNameChange={setName} />;
};
const Child = ({ onNameChange }) => (
<input onChange={(e) => onNameChange(e.target.value)} />
);
This keeps your app logic predictable and manageable.
๐ Use PropTypes or TypeScript
To validate the props being passed, use PropTypes
or better โ switch to TypeScript for type safety.
import PropTypes from 'prop-types';
const Profile = ({ name, age }) => (
<p>{name} - {age}</p>
);
Profile.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number
};
This helps catch bugs during development and improves code clarity.
๐ Avoid Prop Drilling (Use Context)
When you're passing props through multiple nested components just to get data to a deeply nested child, itโs a sign to use the Context API.
Instead of:
<App user="John">
<Layout>
<Sidebar>
<Profile user="John" />
</Sidebar>
</Layout>
</App>
Use:
const UserContext = React.createContext();
const App = () => {
return (
<UserContext.Provider value="John">
<Layout />
</UserContext.Provider>
);
};
const Profile = () => {
const user = useContext(UserContext);
return <p>Hello, {user}!</p>;
};
๐ง Recap: Smart Usage of Props and State
- ๐งฉ Props: For passing data down โ never modify them
- ๐ State: For internal, interactive data โ always manage immutably
- ๐ค Lift state to a common parent when multiple components need it
- ๐งผ Keep state minimal and derive when possible
- ๐ก๏ธ Use validation with PropTypes or TypeScript
Mastering props and state is the first step to mastering React. Use them wisely, and your components will be simple, predictable, and a joy to work with.
๐ What's Next?
- Dive into Context API for global state
- Learn useReducer for advanced state handling
- Explore form handling with controlled components
Props and state are the foundation โ once youโre confident here, the rest of React becomes much easier to build on. ๐ง
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments