Skip to content

Instantly share code, notes, and snippets.

@shrimpy
Created December 10, 2020 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrimpy/05e075ee760a6ee02a5a372aed4c54fd to your computer and use it in GitHub Desktop.
Save shrimpy/05e075ee760a6ee02a5a372aed4c54fd to your computer and use it in GitHub Desktop.
Reactjs: useCallback and React.memo fun - code - 2
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