I'm stumbling across a strange error in my react project. Everything works fine if I go from the home page to the next page. The next page hits an API and updates the Redux store. Now, if I hit the back button or try to go back through the react-router-dom( history.go(-1)
), my page throws this error:
Here is a working CodeSandbox link to the app. Here is how to reproduce the error:
Note that if I don't update the store and then go back to previous page, then everything works fine.
In your file app/screens/EmployeeDetails/employeeDetails.jsx
you have:
useEffect(() => dispatch(fetchEmployee(name)), [])
The implicit return here attempts to use the result of the dispatch as thecleanup
for the useEffect
. Your error occurs because it tries to call the cleanup function since you didn't return undefined
, but you returned a Promise
instead of a function
which the useEffect
expected for it's cleanup (hence the error: 'is not a function').
You should add curly brackets to avoid the implicit return:
useEffect(() => { dispatch(fetchEmployee(name)) }, [])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.