React Basic Hooks - useEffects sample
by kpulkit29
HTML
<div id="root"></div>
<div id="other-div"></div>
CSS
body {
height: 100vh;
width: 100%;
font-family: 'Open Sans', sans-serif;
font-weight: bold;
font-size: 24px;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
}
#root, #other-div {
width: 100%;
}
button {
padding: 10px 20px;
border: none;
border-radius: 6px;
background-color: teal;
color: white;
margin: 50px;
font-weight: bold;
font-size: 1rem;
}
React
const {useState, useEffect} = React;
function Test() {
const [state, setState] = useState(0);
useEffect(() => {
console.log('state updated', state);
}, [state]);
function handle() {
for (let i = 0; i < 100; i++) {
setState((prev) => prev + 1);
}
}
return <button onClick={handle}>CLICK</button>
}
ReactDOM.render( <Test />, document.getElementById('root') );