luisanton-io / recoil-nexus

MIT License
163 stars 13 forks source link

Any ideas on how to observe atom changes? #26

Closed nicolas6422 closed 1 year ago

nicolas6422 commented 2 years ago

Say I want to setup an observer outside of the react render tree like

import { observe } from 'recoil-nexus';

class Class {
  constructor() {
    observe(someAtom, (oldValue, newValue) => {
      // do something
    })
  }
}

Would this be possible? Figured you'd be the best person to ask 😄

luisanton-io commented 2 years ago

Hi, I think I can help but this doesn't look strictly related to this package, could you please post your question on Stack Overflow and link it here? That would help future Googlers as much as yourself, so other engineers could see the question and provide alternative solutions as well.

CyberCyclone commented 1 year ago

@luisanton-io is there a reason this isn't part of the package? It seems like a reasonable utility that covers common use cases.

I'm going to try implementing it myself as part of a forked package, but it would be good to see the way you were thinking it should work.

CyberCyclone commented 1 year ago

I haven't been able to figure out a solution yet mostly because I can't find a function that takes a callback and also subscribes. As in, useRecoilCallback is a once off, it's not a subscription.

I've posted a question on SO: https://stackoverflow.com/questions/76634495/react-recoil-outside-of-react-component-observable-subscriptions

luisanton-io commented 1 year ago

HI @CyberCyclone, I replied to your question.