获取错误:试图合并一个对象,而不是得到[object object]
Getting error: Tried to merge an object, instead got [object Object]
我正在用ReactJS构建一个简单的TODO应用程序,需要用jQuery Sortable对TODO进行排序。我做了大部分的工作,但最后有两个问题不能解决自己:(
代码如下:
componentDidMount: function() {
this.loadDataFromServer();
var jquery_sortable_config = {handle: '#handle'};
jquery_sortable_config.stop = this.handleSort;
this.$jq = jQuery( this.refs.sortable.getDOMNode() );
this.$jq.sortable(jquery_sortable_config);
},
toInt: function(id) {
// I've got <li id={"todo_" + todo.id}></li> so I need to crop todo_
// was - todo_565
// return - 565
return parseInt(id.substring(5));
},
handleSort: function (event) {
var order = this.$jq.sortable('serialize');
// sending new order to server, here is everything OK
$.ajax ({
type: 'POST',
url: 'update_order/',
data: order
});
var reordering = this.$jq.sortable('toArray').map(this.toInt);
this.$jq.sortable('cancel'); // cancel direct DOM change, beacause React can't see it
this.handleDOMUpdate(reordering);
},
handleDOMUpdate: function(reordering) {
console.log(reordering);
// CONSOLE:
// [566, 565]
var newItems = [];
var newState = {};
this.state.data.map(function(item, i, items) {
// for testing there are just 2 elements in state and I just shuffle them
newItems[0] = items[1];
newItems[1] = items[0];
});
newState = newItems;
console.log(newState);
// CONSOLE:
// [Object, Object]
// 0: Object
// id: 566
// status: "done"
// text: "Second task"
// __proto__: Object
// 1: Object
// id: 565
// status: ""
// text: "First task"
// __proto__: Object
// length: 2
// __proto__: Array[0]
// so elements changed positions!
// trying to set new state
this.setState(newState);
// and getting error:
// Uncaught Error: Invariant Violation: Tried to merge an object, instead got [object Object],[object Object]. 10173493_255140104677950_2108691993_n.js:17078
// and futher:
// invariant 10173493_255140104677950_2108691993_n.js:17078
// mergeHelpers.checkMergeObjectArg 10173493_255140104677950_2108691993_n.js:17652
// mergeInto 10173493_255140104677950_2108691993_n.js:17749
// merge 10173493_255140104677950_2108691993_n.js:17558
// ReactCompositeComponentMixin.setState 10173493_255140104677950_2108691993_n.js:6200
// React.createClass.handleDOMUpdate custom_react.js:81
// boundMethod 10173493_255140104677950_2108691993_n.js:6644
// ... etc.
},
这是我的模型:
[
{"status": "", "text": "First task", "id": 565},
{"status": "done", "text": "Second task", "id": 566}
]
所以我有两个问题:
- 我如何设置新的React状态与改变元素的位置?我做错了什么?
你能帮我做一下
this.state.data.map
的逻辑吗?如何迭代所有项目并根据数组reordering[i]
设置它们的新位置?this.state.data.map(function(item, i, items) { // logic });
当你运行setState调用时,你正在用newItems数组newState = newItems;
覆盖你的newState对象,因此你正在传递函数一个数组而不是一个对象。
如果您使用this.setState({ data: newState });
之类的东西,setState调用应该正确工作。这应该也能解决第二个问题。
相关文章:
- object响应时出现对象错误javascript
- Html抓取网站加载错误的J汤Java
- 如何访问npm模块抛出的自定义错误对象[error:[object object]]
- 运行咕噜咕噜咕噜抛出 _.object 不是函数错误
- jqxGrid失败并出现错误“;Object[Object Object]没有方法'jqGrid'&”;
- onclick=“;doSomething([object object])“;未捕获的语法错误:意外的标识符
- Object.defineProperty获取集返回错误的值
- 未捕获的类型错误:Object[Object Object]没有方法'fancybox'
- Ruby on Rails屏幕抓取错误-“;您必须在浏览器中启用javascript才能使用此应用程序"
- Javascript错误Object函数没有方法'url'
- jQuery令牌输入轨道获取错误Uncaught TypeError:Object[Object Object]没有方法
- Array.insert函数错误,因为Object函数Array(){〔本机代码〕}没有方法'插入'
- 附加字符串时,for循环中出现Javascript[Object-Object]错误
- 未捕获的类型错误:属性'$'对象[object Window]的
- 获取 [Object HTMLInputElement] 错误
- jQuery 在使用 ajax 时抓取错误的形式
- 700 由 AJAX 引起的 Google 抓取错误
- HTML javascript函数问题.[object HTMLInputElement]错误输出
- 快速-试图模仿控制器.Get Router.use()需要回调函数,但出现了[object String]错误
- Onload事件抛出'object expected'错误