Created
October 22, 2019 16:13
-
-
Save vicradon/df72f2104ec8e509b49ace309af5eb70 to your computer and use it in GitHub Desktop.
A simple example of useEffect
This file contains hidden or 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 React, {useState, useEffect} from "react" | |
import randomcolor from "randomcolor" | |
function App() { | |
const [count, setCount] = useState(0) | |
const [color, changeColor] = useState(randomcolor) | |
useEffect(() => { | |
const intervalId = setInterval(() => | |
setCount(prevCount => prevCount + 1), 1000 | |
) | |
return () => clearInterval(intervalId) | |
}, []) | |
useEffect(() => { | |
changeColor(randomcolor()) | |
}, [count]) | |
return ( | |
<div> | |
<h1 style={{color: color}}>{count}</h1> | |
</div> | |
) | |
} | |
export default App |
This file contains hidden or 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
<html> | |
<head> | |
<link rel="stylesheet" href="style.css"> | |
</head> | |
<body> | |
<div id="root"></div> | |
<script src="index.pack.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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 React from "react" | |
import ReactDOM from "react-dom" | |
import App from "./App" | |
ReactDOM.render(<App />, document.getElementById("root")) |
This file contains hidden or 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
div { | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
justify-content: center; | |
} | |
h1 { | |
font-size: 3em; | |
} | |
button { | |
border: 1px solid lightgray; | |
background-color: transparent; | |
padding: 10px; | |
border-radius: 4px; | |
} | |
button:hover { | |
cursor: pointer; | |
} | |
button:focus { | |
outline:0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment