React是否可以使用函数参数set {?

时间:2018-04-20 06:31:34

标签: reactjs

您好我正在尝试使用一个函数来设置我的模态状态,而且我有很多模态,所以我正在尝试这样做

这是函数调用

this.openModal('doneModal')

就在这里

 openModal(state) {
  console.log(state);
  this.setState({state : true}, () => console.log(this.state.doneModal));
}

当我记录参数状态时,它按预期显示doneModal,但它没有将状态设置为第二个console.log仍为false。

如果我改为

this.setState({doneModal:true)} the console log shows true. is it not possible to setState as such?

2 个答案:

答案 0 :(得分:2)

在您的代码中添加方括号[]

openModal(state) {
  console.log(state);
  this.setState({ [state]: true}, () => console.log(this.state.doneModal));
}

答案 1 :(得分:1)

为了动态设置状态,您需要将动态变量包装为EntityManager em = getEntityManager(); EntityTransaction transaction = em.getTransaction(); transaction.begin(); Person person = new Person(); person.setAge(25); person.setName("John Ive"); em.persist(person); person.setAge(27); em.merge(person); transaction.commit(); ,如

[]