Mastering React Hooks: A Beginner's Guide to useState
SRINIVAS K
Software Engineer specializing in React, TypeScript, JavaScript and Next.js | Building Scalable Web Applications with a Focus on Performance
?? UseCase of UseState
React Hooks introduced a powerful way to handle state in functional components. Today, let's dive into one of the most commonly used hooks: useState. Whether you're just starting with React or brushing up your skills, understanding useState is fundamental.
?? What is useState?
The useState hook allows you to add state to functional components. Before hooks, state management was only possible in class components. Now, useState gives functional components the power to store and update data dynamically. Here's how it works:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
?? Breakdown:
? Important Concepts to Master:
const [user, setUser] = useState({ name: 'John', age: 25 });
const updateAge = () => {
setUser((prevUser) => ({
...prevUser, // keep other properties
age: 26,
}));
};
const [count, setCount] = useState(expensiveComputation()); // wrong ?
const [count, setCount] = useState(() => expensiveComputation()); // lazy init ?
领英推荐
setCount(count + 1); // it will give you wrong value(old value) ?
setCount((prevCount) => prevCount + 1); // it will give you updated value ?
const [name, setName] = useState('John');
const [age, setAge] = useState(25);
? Best Practices:
???? Practical Example: Toggling Visibility
Let’s take a quick example where we toggle the visibility of a text:
function ToggleText() {
const [isVisible, setIsVisible] = useState(true);
return (
<div>
<button onClick={() => setIsVisible(!isVisible)}>
{isVisible ? 'Hide' : 'Show'} Text
</button>
{isVisible && <p>This is some text!</p>}
</div>
);
}
This example toggles the display of text using useState by updating the isVisible state.
?? Conclusion:
useState is one of the most versatile and essential hooks in React. Whether managing simple counters or complex objects, mastering useState will help you write clean, maintainable functional components.
?? Tip: Always remember to consider performance with lazy initialization and rely on the previous state for updates. Happy coding!
?? What Next?
Now that we know how to store values and re-render the ui to see changes, what if we want to store something that doesn't require re-render? Hint: The next hook also helps with manipulating DOM!
?? On a mission to teach 1 million students | Developer & Mentor | 5,500+ Students ??| JavaScript | React JS | Redux | Python | Java | Springboot | MySQL | Self-Learner | Problem Solver
1 个月Thanks for sharing! ????
Software Engineer specializing in React, TypeScript, JavaScript and Next.js | Building Scalable Web Applications with a Focus on Performance
4 个月checkout the new article to store state without re-rendering the UI: --> https://www.dhirubhai.net/pulse/mastering-react-hooks-beginners-guide-useref-srinivas-k-jsvyc
Full Stack Javascript Developer | Python | GCP | Docker.
4 个月Love this ??