Created
February 10, 2020 06:16
-
-
Save tkssharma/65954b2fc564af04ae86a6340438aa22 to your computer and use it in GitHub Desktop.
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, useMemo } from "react"; | |
const fibonacci = n => { | |
if (n <= 1) { | |
return 1; | |
} | |
return fibonacci(n - 1) + fibonacci(n - 2); | |
}; | |
const MemoComponent = () => { | |
const [num, setNum] = useState(1); | |
const [isGreen, setIsGreen] = useState(true); | |
const fib = useMemo(() => fibonacci(num), [num]); | |
return ( | |
<div> | |
<h1 | |
onClick={() => setIsGreen(!isGreen)} | |
style={{ color: isGreen ? "limegreen" : "crimson" }} | |
> | |
useMemo Example | |
</h1> | |
<h2> | |
Fibonacci of {num} is {fib} | |
</h2> | |
<button onClick={() => setNum(num + 1)}>➕</button> | |
</div> | |
); | |
}; | |
export default MemoComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment