React Basic Hooks - useEffects sample
by JamesPattison
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 Example() {
const [count, setCount] = useState(0);
const incrementCount = () => {
setCount(count + 1);
};
const decrementCount = () => {
setCount(count - 1);
};
return (
<div>
<h1>Count: {count}</h1>
<button onClick={incrementCount}>Increment</button>
<button onClick={decrementCount}>Decrement</button>
</div>
);
}
ReactDOM.render( <Example />, document.getElementById('root') );