Simple enough; all it does is append an object using the setState function which should re-render any components using the state. Here's the problem, this causes an infinite loop...
...this.setState([...this.state, context]);// ^// also updates when state updates, hence re-runs the setState function...
This causes and infinite loop. I could using useEffect(..., []) to make it not be dependant, however, this is a function inside a class, thus it breaks React's rules of hooks.
I have to retain the functionality of this class, so I cannot move the function up into the component(s) as a lot of components use this class and would simply create duplicate code.
What should I do? Is there an alternative of useEffect? Should I be using a different system for this purpose?
Edit: Solution
Convert it to a custom hook!
exportdefaultfunctionuseContextLayer(initialState:Array<listing>){const[state, setState]=useState(initialState);return{removeAll:()=>{...},addContext:()=>{...},// doesn't even need an useEffect anymore}}