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

How does this library compare to unstated-next ? #9

Closed
justingrant opened this issue Dec 19, 2019 · 4 comments
Closed

How does this library compare to unstated-next ? #9

justingrant opened this issue Dec 19, 2019 · 4 comments

Comments

@justingrant
Copy link

@jamiebuilds's unstated-next library seems to address similar use-cases as this library: the ability to have multiple values inside a context object and to have control over which of those values trigger a re-render.

What are the pros and cons of the approach taken by this library vs. unstated-next?

@dai-shi
Copy link
Owner

dai-shi commented Dec 20, 2019

@justingrant The way unstate-next suggests is a pure React way: Create multiple contexts. I don't think they have multiple values inside a single context. Where do you find that description?

Even if there is a library that keeps several values in multiple/single context, there's a use case where selector works better. Selector interface is very powerful, and it can do something like this:

const isYoung = useContextSelector(ctx, s => s.person.age < 10); // it won't trigger a re-render if `age` is changed from `5` to `6`.

@justingrant
Copy link
Author

justingrant commented Dec 20, 2019

@dai-shi - I was thinking of the way that you can put multiple methods and/or properties into an unstated-next Container. Like this:

function useCoordinates() {
  const [x, setX] = useState(0);
  const [y, setY] = useState(0);
  const getRadius = useCallback(() => Math.sqrt(x*x + y*y), 
    [x, y]
   );
  const setCoordinates = useCallback((newX, newY) => { 
    setX(newX);
    setY(newY);
  }, [x, y]);
  return ({
    x,
    setX, 
    y, 
    setY, 
    setCoordinates, 
    getRadius,
  });
}
export const FooContainer = createContainer(useFoo);

But I think I also see what you mean, because in my example above there's not a good way to say "only re-render if x changes but not if y changes. Is that the essence of the difference that you mean?

@dai-shi
Copy link
Owner

dai-shi commented Dec 20, 2019

@justingrant Yes, in your example, if x changes and y doesn't change, a component that only use y will also re-render.

useContextSelector solves it with a proper selector.
useTrackedState in react-tracked solves it without any selector.

I can create some examples if you like.

@dai-shi
Copy link
Owner

dai-shi commented Jan 17, 2020

Closing this. Feel free to re-open it for further discussion.

@dai-shi dai-shi closed this as completed Jan 17, 2020
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