在上一篇文章里,我使用纯js更新object对象,然后在setState里面,这样操作属于原生的,今天在解读React.addons对象的时候,发现有update函数也可以链式更新对象,挺方便的,就是解读起来不那么容易。
import React from 'react'; // 经过种种setState, detail对象是这样的:this.state.detail.iteminfo.state this.setState({ showmessage:true, pmessage:"领取任务成功", detail: React.addons.update(this.state.detail, {iteminfo: {status: {$set: 'doing'}}}) }); // check status {this.state.detail.iteminfo.status=="doing"?<div className="giveup" onClick={this.fio_giveup.bind(this)}>放弃任务</div>:""}
update其他可使用函数:
-
{$push: array}
push()
all the items inarray
on the target. -
{$unshift: array}
unshift()
all the items inarray
on the target. -
{$splice: array of arrays}
for each item inarrays
callsplice()
on the target with the parameters provided by the item. -
{$set: any}
replace the target entirely. -
{$merge: object}
merge the keys ofobject
with the target. -
{$apply: function}
passes in the current value to the function and updates it with the new returned value.有疑问或技术交流,扫描公众号一起讨论学习。
更多React在线学习访问:http://each.sinaapp.com/react/index.html