如何传递道具?

问题描述 投票:0回答:2

创建App2.js组件并将其插入App.js。将s3作为道具z1传递到App2中。将其作为道具带到App2中的页面。

当我通过道具z1时该怎么办?

static getDerivedStateFromProps(props, state) {
    return { z1: props.s3 }
}
reactjs
2个回答
2
投票

您使用以下命令将道具传递给App2

<App2 z1={this.s3Object} />

如果您将prop2扩展为React.Component,您将在其内部访问prop2:

constructor(props) {
  super(props);
  this.props.z1 // this z1 object is the one you passed in from App.js.
  //You can access this.props.z1 anywhere inside of your class, not just constructor
}

如果您打算学习ReactJS,请看这里,https://reactjs.org/docs/components-and-props.html


0
投票

您可以尝试在下面使用

// App.js
<App2 s3Value={this.state.s3 />

在App2.jsx内部

constructor(props) {
        super(props);
    this.state = {
        z1:props.s3Value
       }
}

© www.soinside.com 2019 - 2024. All rights reserved.