25
loading...
This website collects cookies to deliver better user experience
useState
hook we can manage state in our functional components too. So, in this article we will be learning everything that we need to know about useState
to get started with stateful functional components.useState
hook by looking at an example of a simple counter application written using React's functional component.import React, { useState } from 'react';
export function Counter() {
const [count, setCount] = useState(0);
const [msg, setMsg] = useState('Use the below button to increase the count');
return (
<div>
<p>Counter: {count}</p>
<p>{msg}</p>
<button onClick={() => setCount(count + 1)}>Count</button>
</div>
);
}
import React, { Component } from 'react';
export class CounterClass extends Component {
constructor(props) {
super(props);
this.state = {
count: 0,
msg: 'Use the below button to increase the count',
};
}
render() {
return (
<div>
<p>CounterClass: {this.state.count}</p>
<p>{this.state.msg}</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Count
</button>
</div>
);
}
}
constructor(props) {
super(props);
this.state = {
count: 0,
msg: 'Use the below button to increase the count',
};
}
useState
hook.useState(initialState);
useState
hook is an array containing the current state and a function to update the value of current state.const [state, setState] = useState(initialState);
useState
hook.const [state, setState] = useState({
count: 0,
msg: 'Use the below button to increase the count',
});
useState
hook for managing each state. As it is cleaner and easier to maintain.const [count, setCount] = useState(0);
const [msg, setMsg] = useState('Use the below button to increase the count');
useState
hook can slow down the whole application. As you know, in functional components the initial state is declared in the render function and its value updates at every render. This is not a problem in class component as the initial state is defined in the constructor which is called only once at the start.useState
also take function as the argument. the useState
will run this function only once when the component is rendered first time. We can pass the function in useState
like thisuseState(() => {
// Some heavy computation task
});
this.setState
.this.setState({ count: this.state.count + 1 });
this.setState
.this.setState((prevState) => {
return { count: prevState.count + 1 };
});
useState
for each state. We can easily update the value of count by calling the setCount
function like thissetCount(count + 1);
setState
like thissetCount((prevCount) => prevCount + 1);
export function Counter() {
const [count, setCount] = useState(0);
const [msg, setMsg] = useState('Use the below button to increase the count');
return (
<div>
<p>Counter: {count}</p>
<p>{msg}</p>
<button
onClick={() => {
setCount(count + 1);
setCount(count + 1);
}}
>
Count
</button>
</div>
);
}
count
value is still updating by one. This is because the count
value in the setCount
is the same when we render our functional component and count
value doesn't change inside of the function from where it is called. So, in the above code the count
value is same in both setCount
, overriding eachothers value resulting in value of count
increased by just one.setCount
. We can get the desired result as the updated count
value gets stored in the prevCount
and we can use the prevcount
to correctly update the value of count
inside the function.export function Counter() {
const [count, setCount] = useState(0);
const [msg, setMsg] = useState('Use the below button to increase the count');
return (
<div>
<p>Counter: {count}</p>
<p>{msg}</p>
<button
onClick={() => {
setCount((prevCount) => prevCount + 1);
setCount((prevCount) => prevCount + 1);
}}
>
Count
</button>
</div>
);
}
useState
hook to manage all states like thisconst [state, setState] = useState({
count: 0,
msg: 'Use the below button to increase the count',
});
count
. Unlike this.setState
, setState
will overwrite the whole state
object to the new object having only value of count
. You can see in the output of the code below that after clicking the count button the message will get dissappear.export function Counter() {
const [state, setState] = useState({
count: 0,
msg: 'Use the below button to increase the count',
});
return (
<div>
<p>Counter: {state.count}</p>
<p>{state.msg}</p>
<button onClick={() => setState({ count: 1 })}>Count</button>
</div>
);
}
setState
.export function Counter() {
const [state, setState] = useState({
count: 0,
msg: 'Use the below button to increase the count',
});
return (
<div>
<p>Counter: {state.count}</p>
<p>{state.msg}</p>
<button
onClick={() =>
setState((prevState) => {
// Expanding prevState object using spread operator
return { ...prevState, count: 1 };
})
}
>
Count
</button>
</div>
);
}
useState
provides a cleaner and a maintainable way to manage states in an application. After reading this article you are ready to start using useState
in your react projects like a pro.