Skip to content

Instantly share code, notes, and snippets.

@roeib
Last active Sep 29, 2019
Embed
What would you like to do?
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