admin管理员组文章数量:1314039
JavaScript basically doesn't provide watchers for variables. So I wonder how React.js does it.
I'm trying to do the same thing. For objects and arrays, I can use Proxy, like this:
let proxy = new Proxy(arr, {
deleteProperty: function(target, property) {
console.log("Deleted %s", property);
return true;
},
set: function(target, property, value, receiver) {
target[property] = value;
console.log("Set %s to %o", property, value);
return true;
}
});
Maybe it's not a good idea. And I'm still don't know what to do with literals such as Numbers.
JavaScript basically doesn't provide watchers for variables. So I wonder how React.js does it.
I'm trying to do the same thing. For objects and arrays, I can use Proxy, like this:
let proxy = new Proxy(arr, {
deleteProperty: function(target, property) {
console.log("Deleted %s", property);
return true;
},
set: function(target, property, value, receiver) {
target[property] = value;
console.log("Set %s to %o", property, value);
return true;
}
});
Maybe it's not a good idea. And I'm still don't know what to do with literals such as Numbers.
Share Improve this question edited Jun 22, 2019 at 8:55 Gregory Orlov asked Jun 22, 2019 at 8:33 Gregory OrlovGregory Orlov 5372 gold badges7 silver badges15 bronze badges 9-
7
It doesn't, thats why you have to call
setState
. – tkausl Commented Jun 22, 2019 at 8:35 - 1 @tkausl, and what about props? – Gregory Orlov Commented Jun 22, 2019 at 8:40
- This sounds like a XY problem, could you maybe elaborate on what your overall goal is? – Tobias Tengler Commented Jun 22, 2019 at 8:45
- @tobias-tengler, I only want to console.log() when an object, array or literal changed – Gregory Orlov Commented Jun 22, 2019 at 8:53
-
3
@GregoryOrloff As mentioned reactjs is not watching your variables it (just speculating) shallowly pares the new state you pass into
setState
with the previous state and triggers a ponent re-render if the state updated. Maybe you should look into the source code of react, since I don't assume anyone here will have the time to explain thesetState
method to you in-depth... – Tobias Tengler Commented Jun 22, 2019 at 9:16
1 Answer
Reset to default 7The TL/DR was already given in the ments: React does not watch the data, changes are always triggered from "the outside", which means setState
. The state does not even have to change, calling setState
with the same data again already triggers the lifecycle of the ponent.
However, if you use redux or some other state container, it can look as if ponents magically detect changes themself whenever the data used by mapStateToProps
function changes. But react-redux's connect
function merely wraps your ponent with a proxy that listens for events:
- register a listener for every redux action
- pare the current and previous state on every event using strict equal (===)
- if the state is different, call
mapStateToProps
- pare the result of
mapStateToProps
with previous calls using strict equal - if different, trigger a rerendering of the ponent
In order to imitate the behaviour, you would need some kind "thingsDidChange" listener which is triggered by events or continuous (setInterval) check.
本文标签: javascriptHow does react detect changes in statepropsStack Overflow
版权声明:本文标题:javascript - How does react detect changes in stateprops? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741942020a2406198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论