观看除this.state之外的对象/字符串

时间:2018-05-19 05:11:24

标签: react-native watch

ReactNative 中,有没有办法观察除strings对象以外的objectsthis.state的更改?

我听说过Object.watch,但这似乎是一个弃用的桌面解决方案。

1 个答案:

答案 0 :(得分:1)

您可以查看ES6代理:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

示例:



const setHandler = {
  set(obj, prop, value) {
    console.log('Updating property ' + prop + ' to ' + value);
    obj[prop] = value;
    return true;
  }
};


const unmonitoredObject = {a: 0};
const monitoredObject = new Proxy(unmonitoredObject, setHandler);
monitoredObject.a = 1;

//Output: Updating property a to 1