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.

Conditional useRecoilValue usage

See original GitHub issue

How can I conditionally observe recoil state changes? I don’t see that this is currently possible. This is also not easy to do in MobX but it’s possible.

I’d like to be able to pass a falsy value to useRecoilValue

isObserving = false;
const names = useRecoilValue(isObserving  && namesState);

See my MobX question: https://github.com/mobxjs/mobx-react/issues/864

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
drarmstrcommented, May 21, 2020

Internally there is another hook we use for conditional access:

function MyComponent() {
  const {getRecoilValue} = useRecoilInterface_UNSTABLE();
  ...
  const names = isObserving ? getRecoilValue(namesState) : null;
  ...
}

This allows conditional lookups, getting values in loops, after components short-circuit returns, &c. We’re not super keen on the interface, though, it is unstable and will change. I’m hoping to come up with a safer/cleaner approach to publish.

1reaction
acutmorecommented, May 21, 2020

Hi @AjaxSolutions.

I guess I could use this approach? const names = useRecoilValue(noopState);

Yep that’s the way. In Recoil/utils there is a useful helper for this pattern constSelector https://github.com/facebookexperimental/Recoil/blob/c38012d92a292a4a867163cf7b14cfb3debddf01/src/recoil_values/Recoil_const.js#L30-L32

Can use it like this:

const name = useRecoilValue(condition ? nameSelector : constSelector('Mary'));
Read more comments on GitHub >

github_iconTop Results From Across the Web

useRecoilValue(state) | Recoil
Returns the value of the given Recoil state. This hook will subscribe the component to re-render if there are changing in the Recoil...
Read more >
React Recoil useRecoilValue is returning an empty atom
Try change this part of code in Collage.js const adog = useRecoilValue(activeDogAtom); var dog = ""; if(adog === ''){. to this:
Read more >
Get started with Recoil by building a Fantasy app in React ...
To consume this data, we will use the useRecoilValue() function ... Also, by adding this conditional style { backgroundColor: isSelected ?
Read more >
Recoil.js & simple global state
Use this hook when you need to read the value of an atom but not write to it. const myData = useRecoilValue(dataAtom);. Selector....
Read more >
Build your own Recoil - ITNEXT
With the help of useRecoilValue or useRecoilState hooks it is possible to use atoms in a React component: function TextInput() {
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