Skip to content

Instantly share code, notes, and snippets.

@shrimpy
Created December 10, 2020 22:28
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/d850719b167f8475b77fc2b2fb448e7b to your computer and use it in GitHub Desktop.
Save shrimpy/d850719b167f8475b77fc2b2fb448e7b to your computer and use it in GitHub Desktop.
Reactjs: useCallback and React.memo fun - code - 3
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