简体   繁体   中英

react-dom error when going back after updating the redux store

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:

  • Enter some text in the input box.
  • Then hit the search button.
  • Click on browser's button or click on the back button on the page.
  • Observe that the home page is blank. And it shows the above 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM