Coder Social home page Coder Social logo

Comments (4)

dai-shi avatar dai-shi commented on July 20, 2024

@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`.

from use-context-selector.

justingrant avatar justingrant commented on July 20, 2024

@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?

from use-context-selector.

dai-shi avatar dai-shi commented on July 20, 2024

@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.

from use-context-selector.

dai-shi avatar dai-shi commented on July 20, 2024

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

from use-context-selector.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.