Feature request: Second parameter for atomFamily passed to default function
See original GitHub issueFirst of all, thank you guys for an awesome library!
I checked the latest version (0.0.8
) and found your utilities atomFamily
and selectorFamily
very useful, but currently limited.
From your example default
value for an atom
in atomFamily
may be parameterized:
const myAtomFamily = atomFamily({
key: ‘MyAtom’,
default: param => defaultBasedOnParam(param),
});
const myAtom = atomFamily(myAtomKey);
In case above myAtomKey
is responsible for both: atom key and it’s default value. This behaviour makes atomFamily
very limited. What if we add additional parameter to the function which is returned from atomFamily
and simply pass this additional parameter to default
function?
const myAtomFamily = atomFamily({
key: ‘MyAtom’,
default: (key, params) => defaultBasedOnKeyAndParams(key, params),
});
const myAtom = atomFamily(myAtomKey, myAtomSettings);
Now, we may (or may not) pass additional parameters to create atoms with custom default values and this addition will make atomFamily
more useful. Same additional parameter should be added to selectorFamily
as well:
const myGenericNumberState = atomFamily({
key: 'MyNumbers',
default: (key, defaultValue) => defaultValue,
});
const myGenericMultipliedState = selectorFamily({
key: 'MyMultipliedNumbers',
get: (key, multiplier) => ({get}) => {
return get(myGenericNumberState(key)) * multiplier;
},
set: (key, multiplier) => ({set}, newValue) => {
set(myGenericNumberState(key), newValue / multiplier);
},
});
const xAtom = myGenericNumberState('x', 2); // creates x atom with default value 2
const yAtom = myGenericNumberState('y', 3); // creates y atom with default value 3
const x = myGenericMultipliedState('x', 5); // creates selector which multiplies x atom value by 5
const y = myGenericMultipliedState('y', 10); // creates selector which multiplies y atom value by 10
Issue Analytics
- State:
- Created 3 years ago
- Reactions:12
- Comments:19 (7 by maintainers)
Top Results From Across the Web
atomFamily(options) - Recoil
When you call atomFamily() it will return a function which provides the RecoilState atom based on the parameters you pass in. Parameter Type....
Read more >All Configuration Options — Zephyr Project Documentation
Enable support for Bluetooth v4.1 Connection Parameter Request feature in the Controller. CONFIG_BT_CTLR_CONN_PARAM_REQ_SUPPORT · CONFIG_BT_CTLR_CONN_RSSI.
Read more >Integrate Recoil with TypeScript to share your state across ...
Recoil simplifies state management and we only need to create two ingredients: Atoms and Selectors. If you coming from Redux or Redux Toolkit— ......
Read more >Intel® 64 and IA-32 Architectures Software Developer's Manual
Intel 64 processors may support additional software hint to guide the hardware ... the processor went below OS-requested P-state or OS-requested clock ...
Read more >Dynamic atom keys in Recoil - reactjs - Stack Overflow
When you call atomFamily it will return a function which provides the RecoilState atom based on the parameters you pass in.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Example usage of a parameter used for a default might look something like this:
The
evaluatingAtomFamily
example above provides a way to use dynamic call-site-specific defaults for an atom family.