Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active June 16, 2023 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ernestlv/5a5a7eefeef252278d476d09aa2c756e to your computer and use it in GitHub Desktop.
Save ernestlv/5a5a7eefeef252278d476d09aa2c756e to your computer and use it in GitHub Desktop.
Creates a simple local state in react with hooks
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