React App显示props.location.state未定义

我在访问useEffect()挂钩中的props.location.state时遇到麻烦。它是这样的:

useEffect((props) => {
    console.log("location: :", props.location.state); 
}, [props])

它显示props.location.state是未定义的。为什么它是未定义的,我该如何设置?

谢谢。

评论
  • gqui
    gqui 回复

    I think you are using the props from callback, not that props what you want to use. You have to remove argument props from callback.

    useEffect(() => {
        console.log("location: :", props.location.state); 
    }, [props])
    
  • saut
    saut 回复

    The second argument to useEffect is an array of values, the if any one of them changes, the effect should run. As a best practice, you should only put in values here that you need for the effect. I would change it to:

    useEffect(() => {
        console.log("location: :", props.location.state); 
    }, [props.location.state]);
    

    Take a look at the React onEffect docs for more information: https://reactjs.org/docs/hooks-effect.html