0

everyone

while I am working on React Function Component, I got the same problem everyday. it is useeffect dependency problem.

my working flow like the following to get the data by calling the RestAPI

  • define useeffect with empty array [] for component did mount event. I want run the calling api one time
  • add API calling function inside useeffect

my working code like the following.

const DevicesMain = () => { console.log("Devices Main render") const [state, dispatch] = useReducer(reducer, initialState) const [sites, rooms, devices] = useSelector((state) => [state.sites, state.rooms, state.devices]) const reduxDispatch = useDispatch() useEffect(() => { console.log("did mount") if (!sites.get) { reduxDispatch(siteActions.getALL()) } if (!rooms.get) { reduxDispatch(roomActions.getALL()) } reduxDispatch(deviceActions.getALLWithIncludeCount()) return () => { console.log("will unmount") if (subscription) { subscription.unsubscribe() } } }, []) return ( <> ... </> ) } 

after that, always I got the dependency missing Waring like the following.

React Hook useEffect has missing dependencies: 'reduxDispatch', 'rooms.get', 'sites.get', and 'subscription'. Either include them or remove the dependency array react-hooks/exhaustive-deps 

exactly the above warning makes my headache. for some days, I tried to remove this warning.

at first, I added the dependencides that the warning message mentioned to me like the following.

}, [reduxDispatch, rooms.get, sites.get, subscription]) 

after adding like the above, useeffect function is running over 1 time, as that function has rooms.get and sites.get as its dependency.

enter image description here

as you see my the above image, there are serveral did mount log.

secondly, I disabled eslint like the following

// eslint-disable-next-line }, []) 

exactly, I think that this is very basic problem. but still I didn't find what is the best solution for useeffect one time run.

if you give me the best solution, I am thanksful

Regards

1

1 Answer 1

1

Your'e doing perfectly fine. It's a suggestion related to es-lint and not particularly react it self. disabling eslint in this case is perfectly fine and the right approach infact.

Explanation: Eslint basically makes you aware to add dependencies as arguments in array which you are using and which might change in future. As a critical human, your job is to note whether you'd want that behaviour. In this scenario you definitely don't want the behaviour so disabling eslint is the right and recommended way.

Note: Apart from this I would suggest to not listen for subscription on unmount because first of all you haven't defined a subscription variable and secondly there are no subscriptions(listnings) that your'e using in useEffect hook so no need for it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.