观察 this.state 以外的对象/字符串
Watch Objects / Strings other than this.state
在 ReactNative 中,除了 this.state
对象之外,有没有办法监视对 strings
或 objects
的更改?
我听说过 Object.watch
,但这似乎是一个已弃用的桌面解决方案。
您可以检查 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
在 ReactNative 中,除了 this.state
对象之外,有没有办法监视对 strings
或 objects
的更改?
我听说过 Object.watch
,但这似乎是一个已弃用的桌面解决方案。
您可以检查 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