Created
December 10, 2020 22:28
-
-
Save shrimpy/d850719b167f8475b77fc2b2fb448e7b to your computer and use it in GitHub Desktop.
Reactjs: useCallback and React.memo fun - code - 3
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
let _int_val1 = 0; | |
const IntValComp1 = ({initFunc}) => { | |
const [value, setValue] = React.useState(-1); | |
console.log('IntValComp1'); | |
React.useEffect( | |
() => { | |
const val = _int_val1++; | |
if (_int_val1 >= 100000) _int_val1 = 0; | |
setValue(val); | |
initFunc(val); | |
}, | |
[initFunc], | |
); | |
return <div>{`int value one: ${value}`}</div>; | |
}; | |
const ParentComp = () => { | |
const [intVal1, setIntVal1] = React.useState(); | |
const updateValue1 = React.useCallback((v) => setIntVal1(v), []); | |
return ( | |
<div> | |
<IntValComp1 initFunc={updateValue1} /> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment