React Basic Hooks - useEffects sample
by 1004lucifer
HTML
<div id="root"></div>
<div id="other-div"></div>
CSS
body {
height: 100vh;
font-family: 'Open Sans', sans-serif;
font-weight: bold;
font-size: 24px;
display: flex;
justify-content: center;
align-content: center;
flex-wrap: wrap;
text-align: center
}
#root, #other-div {
width: 100%;
}
React
const {useState, useEffect} = React;
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.getElementById('other-div').innerHTML = `You clicked ${count} times`;
var intervalId = setInterval(() => {
console.log('1111');
setCount(count + 1);
}, 1000);
}, []);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
ReactDOM.render( <Example />, document.getElementById('root') );