Last active
June 16, 2023 20:10
-
-
Save ernestlv/5a5a7eefeef252278d476d09aa2c756e to your computer and use it in GitHub Desktop.
Creates a simple local state in react with hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react'; // hook | |
const Root = () => { // functional component | |
const [ state, setState ] = useState(0); // local state for Root component | |
const click = () => { | |
setState( state + 1 ); // re-render Main component | |
} | |
console.log("render Main:", state); | |
return ( // jsx | |
<div> | |
<p>State: { state }</p> | |
<button onClick={ click }>change state</button> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment