Javascript 通过道具

Javascript 通过道具,javascript,reactjs,Javascript,Reactjs,我想从一个组件重定向到另一个组件,并在它们之间传递道具 function MyFirstComponent({survey}) { .... // There is a button that triggers the below if clause. The redirect works but it seems the state is not sent. if (surveyPage === true) { return <Redirect to={{

我想从一个组件重定向到另一个组件,并在它们之间传递道具

function MyFirstComponent({survey}) {
....
// There is a button that triggers the below if clause. The redirect works but it seems the state is not sent.
    if (surveyPage === true) {
        return <Redirect to={{
            pathname: '/detail',
            state: { survey: survey }
        }}

    }

    return("something")

}

导出默认详细信息

如注释中所述,功能组件中的
上不存在道具,因此如果您不解构道具,您可以访问您感兴趣的属性,如下所示:

console.log(props.location.state.survey)

由于您正在解构道具,因此无法再访问
props
属性,只能访问解构的属性。您可以直接获得您感兴趣的价值,如:

function MySecondComponent({ location: { state } }){
    console.log(state.survey)
    return (
        "test"
    )
}

演示

请执行以下操作

函数MyFirstComponent(道具){
如果(surveyPage==真){

返回一个问题我看到的是在功能组件中没有您使用的
。如果我执行
{location:{survey}操作,即使我
console.log(survey)
没有HIS,也会出现相同的问题
它给了我一个
未定义的错误
用于
调查
对不起,我的错误。对象的实际路径是
props.location.state.survey
。因此,要解构它,看起来像
{location:{survey:{state}}}
,这有点过分了。我已经更新了答案并链接到了一个演示。
function MySecondComponent({ location: { state } }){
    console.log(state.survey)
    return (
        "test"
    )
}