Skip to content

Instantly share code, notes, and snippets.

@roeib
Last active September 29, 2019 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roeib/f5b5788e9f4c9c142af42ae66fb0b654 to your computer and use it in GitHub Desktop.
Save roeib/f5b5788e9f4c9c142af42ae66fb0b654 to your computer and use it in GitHub Desktop.
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