Created
December 10, 2020 22:26
-
-
Save shrimpy/05e075ee760a6ee02a5a372aed4c54fd to your computer and use it in GitHub Desktop.
Reactjs: useCallback and React.memo fun - code - 2
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_val2 = 0; | |
const IntValComp2 = ({initFunc}) => { | |
const [value, setValue] = React.useState(-1); | |
React.useEffect( | |
() => { | |
const val = _int_val2++; | |
if (_int_val2 >= 100000) _int_val2 = 0; | |
// use setTimeout here to avoid killing the browser | |
setTimeout(() => { | |
setValue(val); | |
initFunc(val); | |
}, 500); | |
}, | |
[initFunc], | |
); | |
return <div>{`int value two: ${value}`}</div>; | |
}; | |
const ParentComp = () => { | |
const [intVal2, setIntVal2] = React.useState(); | |
return ( | |
<div> | |
<IntValComp2 initFunc={(v) => setIntVal2(v)} /> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment