Richardson
Richardson

Reputation: 2285

Context API function causing an infinite loop?

I am having an issue with context api function when called is causing an infinite loop for some reason. I isolated a lot of code to find out the culprit yet I have to idea what is causing this, and I also made sure there are no dependencies or use effect related to the context action. could putting a context action inside a function in a functional component might cause this ?

const items =  (program: convertedArray) => {
      // this function is causing an infinate loop 
      context.SetCheckBoxGlobalMulti({ asdasdasd: true });
      const itemsElements = Object.values(program)
        .filter((el) => el)
        .flatMap((items) => items);
      return itemsElements;
   };


  return <div>{items(program)}</div>;
};

Upvotes: -1

Views: 359

Answers (1)

Your example is missing the context handling at the parent level, so it is only possible to guess what is happening. Most likely the problem is that context.SetCheckBoxGlobalMulti({ asdasdasd: true }); will cause infinite loop as you create a new object on every call, which react will identify as a change that will cause rerender.

Upvotes: 1

Related Questions