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.

Correct approach to get derived values from state

See original GitHub issue

Hello,

In my store, I have several methods that calculates same derived values from state. I am looking for an optimized way to achive that.

Right now, I can do this:

getDerivedValue: () => {
  return get().listA[get().indexB].value;
}
...
get().getDerivedValue();

But I prefer something like this:

get derivedValue () {
  return get().listA[get().indexB].value;
}
...
get().derivedValue;

Unfortunately, using getter gives only the inital state, returning value doesn’t update with state changes. My computations are not that expensive, but it would be much better to access the value as it was a variable then calling it as a function. My concern is a bit aesthetic but anyway, it would be nice to have something like Mobx’s computed property. Is there a workaround for using getter or could you provide an alternative to achieve this?

Thank you.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

20reactions
samselikoffcommented, Mar 17, 2021

Came across this and surprised to not see the topic of derived state anywhere in the readme. Would love to see how others are handling it!

I was hoping to be able to do something like this:

const useStore = create((set) => ({
  first: 'Daishi',
  last: 'Kato',
  get fullName() {
    return `${this.first} ${this.last}`;
  },
}));

but I guess that’s what you were referring to when you said you don’t think we can support object getters?

The custom hook solution mentioned above isn’t super appealing, just because I’d rather (a) keep my logic near each other and (b) have a single hook for a related group of state + derived data.

Any other thoughts?

8reactions
woehrl01commented, May 9, 2022

I would like to share how I solve this without the need for additional libraries. It’s simple:

https://github.com/pmndrs/zustand/issues/132#issuecomment-1120467721 tl;dr; just use a nested object

Read more comments on GitHub >

github_iconTop Results From Across the Web

Correct approach to get derived values from state #108 - GitHub
Hello,. In my store, I have several methods that calculates same derived values from state. I am looking for an optimized way to...
Read more >
Understand Derived State in React - In Plain English
When a prop is used to initiate a state, that state is called a derived state. We can see the prop as a...
Read more >
You Probably Don't Need Derived State – React Blog
This technique is known as memoization. Using derived state for memoization isn't necessarily bad, but it's usually not the best solution.
Read more >
What is derived state in React, and why is it important?
Derived state is a state which mainly depends on props. static getDerivedStateFromProps(props, state) { if (props.value !== state.
Read more >
Deriving Data with Selectors - Redux
Less logic is needed to calculate those additional values and keep them in sync with the rest of the data; The original state...
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

No results found

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