我可以在reactjs
的onClick
-event上创建新组件。 我已经试过这样:React - 返回组件onClick事件
我的部件,其中该点击事件发生:
...
<li onClick={this.edit.bind(this)}><span className="glyphicon glyphicon-pencil"></span> Edit entry</li>
...
这是edit
方法:
edit(event) {
return <CreateFreightEntryModal key={"freightEditModal" + this.state.freight.ID} openOnStartup="true" modalId={"freightEditModal" + this.state.freight.ID} />
}
不引发错误,但该组件没有被创建。我做错了什么,或者这不是真的推荐?
onlick函数根本不编辑状态,你只是返回一个组件。你应该让edit函数切换一个'edit'状态,使'CreateFreightEntryModal'组件被渲染,而不是通常在那里。 – Joe
你在哪里期待返回的元素会被渲染?使用状态布尔和onclick编辑按钮使用'setState'并用true更新,使用该变量渲染组件,检查[文档的有条件渲染部分](https://facebook.github.io/react/docs /conditional-rendering.html) –
如果这只是一个元素,请将标志保持在状态并使用'{flag? component:null}'in render – Rajesh