Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectors? #25

Closed
anirban09 opened this issue May 19, 2019 · 3 comments
Closed

Selectors? #25

anirban09 opened this issue May 19, 2019 · 3 comments

Comments

@anirban09
Copy link

How would one go about adding selectors to a container?

For example, if a component wanted to use only the count variable from this container, it shouldn't re-render when anotherCount is changed.

function useCounter(initialState = 0) {
  let [count, setCount] = useState(initialState);
  let [anotherCount, setAnotherCount] = useState(initialState);

  let decrement = () => setCount(count - 1);
  let increment = () => setCount(count + 1);

  let decrementAnother = () => setAnotherCount(anotherCount - 1);
  let incrementAnother = () => setAnotherCount(anotherCount + 1);

  return { count, decrement, increment, anotherCount, decrementAnother, incrementAnother };
}

let Counter = createContainer(useCounter);
@w10036w
Copy link

w10036w commented May 19, 2019

"selector" is designed for solo-store solutions like redux, since they need to avoid rerendering for unaffected nodes.

For multi-store case, you'd better have another container if these states are irrelevant.
To prevent unexpected rerendering, check memo or useMemo.
As well you can expose the setter/reducer of one store so you can dispatch the actions, triggered by the change of another store state, inside your component logic

@anirban09
Copy link
Author

There do seem to be some solutions about using only part of what useContext returns in this comment.

@anirban09
Copy link
Author

I see that there's documentation in the README for this, under the "Optimizing components" section. Was that not there before, or did I miss it earlier? 😅
Anyway, closing this then.

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

No branches or pull requests

2 participants