简体   繁体   English

使用道具将对象从一个组件传递到另一个组件

[英]Passing an object from one component to another using props

I am trying to pass a whole object from one component to another on button click. 我试图在单击按钮时将整个对象从一个组件传递到另一个组件。

I have tried using Link and Router, but I still cannot see the object inside the props of the destination component. 我尝试使用链接和路由器,但仍然看不到目标组件的道具内的对象。

I am using the onClick handler handleReportIncident to pass an available object while Routing to this component. 我正在使用onClick处理程序handleReportIncident在路由到此组件时传递可用对象。 The handleReportIncident function is bound at the top. handleReportIncident函数绑定在顶部。

    handleReportIncident() {
        const { trip } = this.state;
        this.props.history.push(`/app/new`)
        return (
            <div>
                <New trip={trip} />
            </div>
        )
    }

    render() {

        return (

                    <Button
                        variant="contained"
                        color="primary"
                        className={classes.toolbarButton}
                        onClick={this.handleReportIncident}
                    >
                        <AddIcon />
                        Incident
        </ Button>
)}

Inside of New component I have : 在新组件内部,我有:

class New extends Component {
render() {
const {trip} = this.props;
console.log("the trip is", trip);
return(
...
)
}
New.propTypes = {
    trip: PropTypes.object.isRequired,
};

Note: I am using material UI as a theme. 注意:我正在使用实质性UI作为主题。

The Log inside the New component is the trip is undefined . New组件内部的Log是行程undefined

You can wrap your desired object in the state of the Link. 您可以将所需的对象包装在链接状态。 See example below. 请参见下面的示例。

// Main Component from which you want object passed //要从中传递对象的主要组件

render() {

  return (
    <Link to = {{
      pathname: '/app/new',
      state: {
        trip: this.state.trip
      }
    }}>
      <AddIcon /> Incident 
    </Link>
  )
}

// New Component get desired object from this.props.location.state //新组件从this.props.location.state获取所需的对象

class New extends Component {
render() {
const { trip } = this.props.location.state
console.log("the trip is", trip);
return(
...
)
}
New.propTypes = {
    trip: PropTypes.object.isRequired,
}

For more info on how it works head over to this tutorial: https://tylermcginnis.com/react-router-pass-props-to-link/ 有关其工作原理的更多信息,请转到本教程: https : //tylermcginnis.com/react-router-pass-props-to-link/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个组件传递道具 - Passing props from another component 在ReactJS中将道具从一个组件传递到另一个组件将返回undefined - Passing props from one component to another in ReactJS returns undefined 反应,将未定义的道具从一个组件路由到另一个组件? - React, route passing props undefined from one component to another? 将数据作为道具从另一状态传递到一个组件 - Passing data as props to one component from another's state 使用typescript在将prop从一个组件传递到另一个组件时发生错误,反之亦然 - Error Coming when passing props from one component to another and vice - versa in react using typescript 使用router.push进行转换时,将道具从一个组件传递到另一个组件 - Passing props from one component to another when the transition happens using router.push object 使用道具从刀片传递到 vue 组件时未定义 - object is undefind when passing from blade to vue component using props 将 React 中的道具从一个文件传递到另一个文件 - Passing props in React from one file to another 从另一个组件传入道具后,组件中的功能不起作用 - Function in component not working after passing in props from another component 对象在将道具从一个组件传递到另一个组件时,不能作为反应子对象(带有键的找到的对象)有效 - objects are not valid as a react child (found object with keys) while passing props from one component to other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM