如何使用反应不可变助手在现有对象中插入新对象

时间:2017-03-06 05:13:14

标签: javascript ruby-on-rails reactjs react-rails

我正在使用react_rails gem在前端使用Rails上的应用程序。使用状态我有来自服务器的道具一个看起来像这样的对象:{assignments: {assignment1: {another_collection}, {response: {key: value, key1: value1, etc: etc }},{assignment2: {collection {}{}{}}}}当我创建新的响应对象时从服务器返回json我必须更新状态请注意第二个对象没有创建的响应对象。我正在尝试以下代码来更新状态。

 addResponse(assignment, response) {
    let indexa = this.state.assignments.indexOf(assignment);
    let assignments = React.addons.update(this.state.assignments, {[indexa]: {   response: {$push: [response]}}});
    this.setState({assignments: assignments})
}

我在控制台上收到以下错误: “推动成为一个阵列的预期目标;得到了Cliente indica que esta botando aceite por debajo。”这是键赋值的on值:对象赋值。在示例对象上,您可以看到服务器已经创建并发送了一个响应对象。怎么解决这个?

0 个答案:

没有答案