在 ReactNative 中,有没有办法观察除strings
对象以外的objects
或this.state
的更改?
我听说过Object.watch
,但这似乎是一个弃用的桌面解决方案。
答案 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