-
-
Notifications
You must be signed in to change notification settings - Fork 87
Subscribe to deep-nested global state properties. #4
Comments
Hi @theKashey, The Object key listener is deliberately shallow. Can you elaborate on the performance issues that you believe this to cause? I am aware that it will cause re-renders on components that access
I do want to be clear that ReactN deliberately does not aim to be as full-featured as Redux. There are trade offs of meaningful features in exchange for a shallow learning curve, less boilerplate, and more maintainable code. It is up to each project to determine what is more important. There is no one global state management solution that applies to every project. 👍 I would not personally consider Thanks for taking the time to look at the repository. I look forward for more feedback. |
I have added the behavior to the documentation here. Let me know if this closes your issue. |
You may just keep tracking nested keys. At lest for objects, as long for arrays without proxy that could be not so easy. <div>{global.x}</div> - I should update on `x` change
<div>{global.x.y}</div> - I should update on `x.y` change, but could ignore `x` chage
<div>{global.x.y} + {global.x.z}</div> - I should update on `x.y` and `x.y` change
....
<div>{someFunction(global.x)}</div> - oh. I don't know. To be honest - I dont know how to manage key tracking on the "render" level. |
It is not possible to know whether I accessed const a = this.global;
const b = a.x;
const c = b.y;
return c; // I only want to subscribe to this.global.x.y changes. Boilerplate can solve this issue, but I am strictly opposed. At that point, one should just be using Redux. It is possible to listen to only the deepest accessed properties. If I accessed This would work in all cases except where both the object and a property on that object are used. I cannot think of a use case for this, outside of |
Optional boilerplate may be acceptable for the sole purpose of performance benefits, instead of necessity. Something like It doesn't add boilerplate to the typical user case, but I'm not sure how intuitive it is. I'm also not completely sold on adding listeners to nested objects, What if that object is an instance, such as new Promise? Returning a modified clone sounds like it can cause bugs. I do like the idea in concept, though. I've also just opened this withGlobal HOC issue for discussion on it as a viable choice for optional boilerplate. |
Hey @theKashey , I don't mean to spam you with these discussions. I've just released deep-proxy-polyfill in an attempt to get close as possible to Proxy behavior. I may change the syntax a bit, but that would be a rough draft of how deep-nested subscriptions would work. It's passing tests that it is subscribing correctly. Some performance needs to be taken into consideration (having getters on every parent object firing), but ultimately I think it's a good starting point. With a community go-ahead, I'll implement it in ReactN. |
Looking good! If you will also use |
This would be so useful if implemented int ReactN! |
How about using [state, updateState] = useUpdateState("globalState")
updateState({action: "action-to-perfom", field: "field-to-update", value: "value-to-update"}) supported operations being |
setGlobal({
user: {isLoggedIn: true, info: {name: "peter", emails: ["[email protected]"]}, }
})
function User(props){
[user, updateUser] = useUpdateState("user");
let editName = () => {
updateUser({action: "assign", field: "info.name", value: "jason"})
// Or simply updateUser({field: "info.name", value: "jason"})
}
let addEmail = () => {
updateUser({action: "push", field: "info.email", value: "[email protected]"})
}
let removLastEmail = () => {
updateUser({action: "pop", field: "info.email"})
}
let removeEmail = () => {
updateUser({action: "remove", field: "info.email", value: "[email protected]"})
}
let filterEmails = () => {
updateUser({action: "filter", field: "info.email", value: (email) => email != "[email protected]"})
}
let logOutUser = () => {
updateUser({action: "assign", field: "isLoggedIn", value: false})
// Or simply updateUser({field: "isLoggedIn", value: false})
}
} |
Object key usage tracking is working only one level down, thus would be a source of performance issues for a wide set of cases, normally manageably by Redux.
Take a look how it was made in react-easy-state. Unfortunately (to you), that a-year-old-library is much more usable than this brand-new
The text was updated successfully, but these errors were encountered: