[英]How can i pass data between two classes, components in ReactJS?
我有兩節課。 我想將數據從一個 class 傳遞到另一個。 我怎樣才能做到這一點?
class A extends React.Component {
render() {
return (
<B data={this.getData()} />
);
}
}
class B extends React.Component {
//How can i consume "data" in here?
}
您可以使用以下命令訪問 class B 中的數據:
this.props.data
您的B
組件將獲得一個名為data
的道具,您可以在B
組件中使用this.props.data
訪問該道具。
https://reactjs.org/tutorial/tutorial.html#passing-data-through-props
class A extends React.Component {
getData() {
return [ { "test": "data" } ];
}
render() {
return (
<B data={this.getData()} />
);
}
}
class B extends React.Component {
render() {
return (
<label>test: <span>{this.props.data['test']}</span></label>
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.