在组件之间传递数据使用reactjs
问题描述:
我有一个像下面的div。在组件之间传递数据使用reactjs
<div className="horiz_center" onClick={this.textType.bind(this,'text')}>
<img src={StarIcon} className="post_type_icon"/>
<a className="post_type_text">Text</a>
</div>
我可以使用此功能
textType(postType) {
this.setState({postType});
}
我的问题是,如果我想在另一个组件使用postType
的价值,我怎样才能把它传递给该组件?
答
const {postType} = this.state;
<MyComponent postType={postType} />
而在你的组件(MyComponent
)访问您的属性,如
this.props.postType
您使用'props'了点。尝试阅读更多[这里](https://facebook.github.io/react/docs/components-and-props.html) – ickyrr