Correct approach to get derived values from state
See original GitHub issueHello,
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:
- Created 3 years ago
- Reactions:13
- Comments:22 (4 by maintainers)
Top 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 >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
No results found
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
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:
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?
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