GithubHelp home page GithubHelp logo

Comments (4)

davidmccabe avatar davidmccabe commented on May 8, 2024 3

Here's how I would do it:

function useCounter() {
  const [value, setValue] = useRecoilState(countAtom);
  const add = useCallback(() => {setCount(x => x + 1)});
  return {value, add};
}

To make a family of atoms, just create a memoized function that returns a distinct atom for each state:

memoize(id => atom({
 key: `whatever${id}`,
 default: 123,
}));

where memoize is any memoization function you provide.

We are going to open-source a utility called atomFamily/selectorFamily that does this for you in the near future.

from recoil.

xialvjun avatar xialvjun commented on May 8, 2024 2

Thanks.
In fact, I'm so dumb that I missed the point of your answer. I used to think we need to copy code in every component like:

// counterAtom.js
export const counter = atom({
  key: 'counter',
  default: 0,
});
export const operations = (setValue) => ({
  inc: () => setValue(s => s+1),
  dec: () => setValue(s => s-1),
});

// myCom.jsx --- copy the code everywhere
const [value, setValue] = useRecoilState(countAtom);
const operations = useMemo(()=>counterOperations(setValue));

I forgot to use hook to extract logic to outside of component.
Just a mark.

from recoil.

davidmccabe avatar davidmccabe commented on May 8, 2024

Actually I think it's already in the repo, the build just isn't configured to include it. If you can figure out how to set up the build to include the RecoilUtils module as a separate module e.g. recoil/utils in the built output, that'd be a nice PR.

from recoil.

tony-go avatar tony-go commented on May 8, 2024

I give a try #33

Hope it help :)

from recoil.

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.