Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Computed properties within a useReducer() store. #809

Open
nyenye opened this issue Apr 21, 2020 · 0 comments
Open

Computed properties within a useReducer() store. #809

nyenye opened this issue Apr 21, 2020 · 0 comments

Comments

@nyenye
Copy link

nyenye commented Apr 21, 2020

Hi @kentcdodds

I followed you article in here: https://kentcdodds.com/blog/how-to-use-react-context-effectively

and it has been a huge help, but as the title says, I am kind of missing computed properties or getters. Let me give you an example:

Imagine you have an array of items, and you can select one to do stuff with it. So you could have this state on a reducer:

const state = {
  items: <Item>[],
  selectedItem: null | Item
}

which duplicates state, or you could have something like this:

const state = {
  items: <Item>[],
  selectedItemId: null | string
}

and from there derive the selectedItem in a computed property or getter of sorts, and avoid duplicating state.

Do you have any idea how this could be accomplished? Do you think it's even worth it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant