简体   繁体   中英

Does the componentDidMount useEffect hook run when comming back from another react-link from the same project?

Inside a React Project. Let's say you start at the FavoriteList -page, from there you go to the ItemInfo -page. My question is, when you go back to the Favorites-page, is the useEffect(() => {}, []) hook (similar to the ComponentDidMount ) load again? Because it's not loading for me:'( It only loads the very first time.

Use effect always runs on the first render of a component, then after that depends on what you pass as your second argument.

If you want useEffect to run ONLY on the first render of the component, use this:

useEffect(() => {}, [])

If you want useEffect to run on EVERY render, use this (no second argument):

useEffect(() => {})

If you want useEffect to run on the first render of the component, AND on state(s) changes, use this:

useEffect(() => {}, [state, anotherState])

If you are trying to avoid useEffect rendering on the first render, you need to use a UseRef hook. This post will tell you how to go about doing this.

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