我有一个项目列表(ProjectActivityList
),每个项目都有编辑按钮。单击它可打开模式以编辑项目。模态接受ID。
var ProjectActivities = React.createClass({
onEditItem: function(id){
// I want to pass the ID to modal and open it
},
render: function(){
return (
<div>
<ProjectActivityList items={this.state.activities} onEdit={this.onEditItem}/>
<Modal />
</div>
)
}
});
如何打开模式传递给它的ID(onEditItem
)?
答案 0 :(得分:0)
您希望将其作为道具传递并设置当前元素的状态。通过设置状态,将执行重新渲染(如果需要,在这种情况下),因此模态将接收新的id。
var ProjectActivities = React.createClass({
onEditItem: function(id){
// I want to pass the ID to modal and open it
this.setState({editingId: id});
},
render: function(){
return (
<div>
<ProjectActivityList items={this.state.activities} onEdit={this.onEditItem}/>
<Modal id={this.state.editingId} />
</div>
)
}
});