question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using getter to calculate computed values

See original GitHub issue

Hey!

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:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

43reactions
woehrl01commented, May 10, 2022

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:

const [useAuth] = create((set, get) => ({
    user: {username: undefined, authLevel: 0},
    computed: { //yes, just use a nested object, which can be easily used in `Object.assign`
       get isSignedIn: () => !!get().user.usernamecomponents?
    }
})}

//usage
const isSignedIn = useState(s => s.computed.isSignedIn)
14reactions
dai-shicommented, Sep 11, 2020

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.

const useAuth = create((set, get) => ({
    user: {username: undefined, authLevel: 0},
    signedIn: () => !!get().user.username
})}

// in components
function Foo() {
  const signedIn = useAuth(state => state.signedIn())

// vanilla non-reactive
const signedIn = useAuth.getState().singedIn()
// vanilla reactive
useAuth.subscribe(signedIn => console.log(signedIn), state => state.signedIn())

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found