使用 reactjs 在组件之间传递数据

Posted

技术标签:

【中文标题】使用 reactjs 在组件之间传递数据【英文标题】:Passing data between components using reactjs 【发布时间】:2017-11-07 02:26:39 【问题描述】:

我有一个像下面这样的 div。

<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,我该如何将它传递给那个组件?

【问题讨论】:

你使用props。尝试阅读更多here 【参考方案1】:
const postType = this.state;
<MyComponent postType=postType />

并在您的组件 (MyComponent) 中访问您的属性,例如

this.props.postType

【讨论】:

以上是关于使用 reactjs 在组件之间传递数据的主要内容,如果未能解决你的问题,请参考以下文章