Comments (4)
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.
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.
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.
I give a try #33
Hope it help :)
from recoil.
Related Issues (20)
- selector seems not get new value in sometimes HOT 1
- Under active maintenance? HOT 16
- How to avoid "Setting selectors within atomicUpdate is not supported" when using useRecoilTransaction__UNSTABLE
- RecoilLoadable contains Promise value will never change state from 'loading' to other states.
- RecoilURLSync has unnecessary encodeURIComponent for search serializer
- Testing custom hook with recoil atom inside
- There is a bug on your website HOT 1
- onSet is not triggered when using `useRecoilState` HOT 2
- If value is a NaN, it causes an infinity render HOT 1
- Nested RecoilRoot using initializeState has unexpected results
- `jotai-recoil-adapter`: a community project for making life easier when migrating from Recoil to Jotai HOT 1
- [Question]how to return extra info with async atom
- Android: Setting recoil state yields "ownKeys target is non-extensible but trap result keys differ from target keys"
- Feature request: Selector-like hook for components HOT 2
- Problems with Continuous Updates HOT 1
- Using RecoilRoot via npm HOT 2
- Recoil-Sync syncEffect Jest error
- can't use the search box to jump to destination on the official website
- recoil state getting reset after auth with different platforms HOT 1
- Japanese Translation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from recoil.