React addons.update setState 链式更新object属性

在上一篇文章里,我使用纯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 in array on the target.
  • {$unshift: array} unshift() all the items in array on the target.
  • {$splice: array of arrays} for each item in arrays call splice() on the target with the parameters provided by the item.
  • {$set: any} replace the target entirely.
  • {$merge: object} merge the keys of object 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值