i have and useEffect inside my component
useEffect(() => {
const interval = setInterval(() => {
setQueueState((pre) => {
return pre.map((line) => {
if (line.length === 0) return [];
line[0].items = line[0].items > 0 ? line[0].items - 1 : 0;
if (line[0].items > 0) {
return [
line[0],
...line.slice(1).filter((person) => person.items > 0)
];
} else {
return [...line.slice(1).filter((person) => person.items > 0)];
}
});
});
}, 4000);
return () => {
clearInterval(interval);
};
}, []);
What it basically does is to loop through an array of checkout lines and reduce the number of items of the first person in each line by 1. Now the problem with Strict Mode is that it gets reduced 2 times. I know it’s because Strict Mode renders a component 2 times. I just want to know a way to avoid this and still use Strict Mode.
2
Answers
thanks to the coment from Giorgi Moniava i rewrote the hook so it does not mutate the state and now it works as i wanted this is how the hook looks now
As you pointed out, there are 2 renders with Strict mode on development. On my project, I create a hook called
useEffectOnce
.and I use it like this: