question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Second parameter for atomFamily passed to default function

See original GitHub issue

First 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:open
  • Created 3 years ago
  • Reactions:12
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
drarmstrcommented, Sep 3, 2020

Example usage of a parameter used for a default might look something like this:

const fieldState = evaluatingAtomFamily({
  key: 'MyState',
  get: (field, defaultValue) => ({get}, fieldValue) => fieldValue ?? defaultValue,
});


const x = useRecoilValue(fieldState('email', 'some@email.com'));    
1reaction
drarmstrcommented, Jan 7, 2021

The evaluatingAtomFamily example above provides a way to use dynamic call-site-specific defaults for an atom family.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found