Created
February 12, 2020 17:35
-
-
Save r3dm1ke/082aa7cc2cb1ac756f0a2a38e851a2a4 to your computer and use it in GitHub Desktop.
Example without useMemo
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} from 'react'; | |
| function App() { | |
| const [length, set_length] = useState(3); | |
| const [name, set_name] = useState('John Doe'); | |
| return ( | |
| <> | |
| <input value={name} onChange={e => set_name(e.target.value)} /> | |
| <NameDisplay name={name}/> | |
| <hr /> | |
| <input value={length} onChange={e => set_length(Number(e.target.value))} /> | |
| <FibDisplay length={length} /> | |
| </> | |
| ); | |
| } | |
| function FibDisplay({length}) { | |
| console.log('Calculating numbers & rerendering...'); | |
| const numbers = [1, 1]; | |
| for (let i = 2; i < length; i++) { | |
| numbers[i] = numbers[i - 1] + numbers[i - 2]; | |
| } | |
| return <p>{length} numbers of the fibonacci sequence: {numbers.join(', ')}</p>; | |
| } | |
| function NameDisplay({name}) { | |
| console.log('Rerendering name...'); | |
| return <p>Your name is {name}</p>; | |
| } | |
| export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment