Created
March 17, 2020 07:56
-
-
Save krishnadevz/28616f5d4bf080b0c061e7226b7d2083 to your computer and use it in GitHub Desktop.
useEffect hook in functional component reactjs
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} from "react" | |
import randomcolor from "randomcolor" | |
function App() { | |
const [count, setCount] = useState(0) | |
const [color, setColor]=useState(randomcolor()) | |
function increment() { | |
setCount(prevCount => prevCount + 1) | |
} | |
function dibs() { | |
setCount(prevCount => prevCount *2) | |
} | |
//Callbacks for useEffect for changing color | |
useEffect(()=>{ | |
setColor(randomcolor()) | |
},[count]) | |
return ( | |
<div> | |
<h1 style={{color: color}}>{count}</h1> | |
<button onClick={increment}>Increment</button> | |
<button onClick={dibs}>Doubly</button> | |
</div> | |
) | |
} | |
export default App | |
//This codeSnippet doing follwing things when we increment then number increment with color changing and for doubling also same🔽 | |
//incrementing number by 2 and by using effect callbacks hooks i studied useEffect hooks here hope you guys also get it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment