EN
React - setState with a callback function (useState hook)
0 answers
0 points
Hi!
I want to display some information after updating the state
value in React functional component. Is there a way to call setState()
with a callback function that console.log some text?
This is what I want to do:
xxxxxxxxxx
1
import React, { useState } from 'react';
2
3
const App = () => {
4
const [state, setState] = useState(0);
5
6
const handleClick = () => {
7
setState(1, () => {
8
console.log('state updated succesfully');
9
});
10
};
11
12
return (
13
<div>
14
<h1>{state}</h1>
15
<button onClick={handleClick}>update</button>
16
</div>
17
);
18
};
19
20
export default App;
0 answers