Last active
September 29, 2019 09:51
-
-
Save roeib/f5b5788e9f4c9c142af42ae66fb0b654 to your computer and use it in GitHub Desktop.
This file contains 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, useRef } from "react"; | |
import ReactDOM from "react-dom"; | |
function App() { | |
const [count, setCount] = useState(0); | |
const isFirstRun = useRef(true); | |
useEffect(() => { | |
if (isFirstRun.current) { | |
//skip firstRun | |
isFirstRun.current = false; | |
return; | |
} | |
//here we are sure count has been change | |
console.log("count", count); | |
}, [count]); | |
return ( | |
<div className="App"> | |
<h1>counter: {count}</h1> | |
<button onClick={() => setCount(count => count + 1)}> | |
click | |
</button> | |
</div> | |
); | |
} | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment