Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Created February 12, 2020 17:35
Show Gist options
  • Select an option

  • Save r3dm1ke/082aa7cc2cb1ac756f0a2a38e851a2a4 to your computer and use it in GitHub Desktop.

Select an option

Save r3dm1ke/082aa7cc2cb1ac756f0a2a38e851a2a4 to your computer and use it in GitHub Desktop.
Example without useMemo
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