I have a react hook.
This hook has sub-hooks, that when generated, generate for themselves an ID.
This ID is always unique, as it is simply increased by 1 each time a new sub-hook is created.
const App = () => {
const [ idCounter, setIdCounter ] = React.useState(0);
const genId = () => {
setIdCounter( id => id + 1 );
return `ID-${idCounter}`;
}
const SomeComponent = () => {
const [ componentId, setComponentId ] = React.useState(null);
React.useEffect(() => {
let generatedId = genId();
setComponentId( id => generatedId );
console.log(`generated '${generatedId}'`)
}, []);
return <div>nothing works</div>
}
return <SomeComponent />
};
This loops and logs the generated id over and over again. Why on earth would it do this?
useEffect() is dependent on... nothing!! It should run only once, no?
How can I get this not to happen? I would like to be able to create several of SomeComponent from within App in the future.