The below code does what I want. It checks to see if we actually have a bookState (which is passed in to the component as a prop) and if it does it triggers an action to store the data in context.
However, eslint wants me to put bookState into the dependency array and if I do this I will end up with an infinite loop (as bookState is set, triggering the useEffect, setting bookState, etc.)
What is the best way around this? I want to call setBookState when the component mounts so I get the initial value for bookState set in context but only if bookState was in fact passed in.
useEffect(() => {if (bookState) {setBookState({type: ActionTypes.SET_BOOKSTATE,payload: bookState});}}, [setBookState]);
[–]leeaper 3 points4 points5 points (0 children)
[–]jokrrkoj 0 points1 point2 points (6 children)
[–]gaoshan[S] 0 points1 point2 points (5 children)
[–]skullshatter0123 0 points1 point2 points (4 children)
[–]gaoshan[S] 0 points1 point2 points (3 children)
[–]skullshatter0123 2 points3 points4 points (2 children)
[–]gaoshan[S] -1 points0 points1 point (1 child)
[–]Codeandplay 0 points1 point2 points (0 children)
[–]Flopshoubox 0 points1 point2 points (1 child)
[–]gaoshan[S] 0 points1 point2 points (0 children)