Using getter to calculate computed values
See original GitHub issueHey!
Is it ok to use the getter to return a computed value in a function? Since using it in a value doesn’t work?
Example:
const [useAuth] = create((set, get) => ({
user: {username: undefined, authLevel: 0},
// isSignedIn: !!get().user.username // doesn't work because it's executed before the store is created, so get() returns undefined right?
isSignedIn: () => !!get().user.username // should work, just call isSignedIn() instead of using as a value, in the components?
})}
Also, I feel like computed values are a cool features for state management libraries to have out of the box, are there planes to add it? Do you guys need help with it somehow?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:19 (7 by maintainers)
Top Results From Across the Web
Computed properties with property getters - OkKotlin
Computed properties make beautiful code We can define our own getter and setter methods to make fields return a computed value. The problem...
Read more >Getters and Setters in Swift - Computed Properties
Getters and setters in Swift are the methods for accessing and modifying computed properties. The getter computes a value on-demand.
Read more >Getters and setters - Flutter by Example
Getters. Getters are often useful to set computed properties. In other words, instance variables that rely on other, non-constant variables.
Read more >Computed Properties - Vue.js
Here we have declared a computed property publishedBooksMessage . The computed() function expects to be passed a getter function, and the returned value...
Read more >Deriving information with computeds - MobX
Computed values can be created by annotating JavaScript getters with computed . Use makeObservable to declare a getter as computed.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d like to share that it’s definitely possible to have computed fields, it just needs to be constructed differently to don’t fall into the
Object.assign
issue. But this requirement makes the code even more readable and explicit:Summary: So there are two topics in this issue. a) Support object getters in store b) Chaining computed values (aka atoms)
For b), we are planning a new project coming soon, and it’s outside the scope of zustand.
For a), it’s a possible enhancement, but the implementation might be a bit tricky. I’m not sold much, because we don’t support b) anyway.
We should be able to do this without object getters.
Now, reading the thread from the beginning again, I noticed I misunderstood the original issue. The OP never said object getters. So, the above example would be the answer.
That said, I think it’s fairly OK to mark this issue as resolved.