Replies: 3 comments 3 replies
-
Going to have to setup something similar to instantiate an instance of the Zero client, which depends on the |
Beta Was this translation helpful? Give feedback.
-
@marbemac your approach with When the third-party lib's object references can change, they're state. I wouldn't use memos, refs, or exports for that. When you need the ability to tear down a third-party lib's objects, use An example with both of these: export const someThirdPartyClientAtom = atom('someThirdPartyClient', () => {
const { jwt } = injectAtomValue(authAtom);
const signal = injectSignal(null as ReturnType<typeof createSomeThirdPartyClient> | null);
injectEffect(
() => {
const client = createSomeThirdPartyClient()
signal.set(client) // the client is the state of this atom
return () => client.destroy() // handle cleanup
},
[jwt], // rerun on jwt change
{ synchronous: true } // run the effect immediately
);
return signal;
}); (Could also do it like this to prevent the atom's type from being nullable:) export const someThirdPartyClientAtom = atom('someThirdPartyClient', () => {
const { jwt } = injectAtomValue(authAtom)
// define the client immediately to prevent the type from being nullable:
const signal = injectSignal(() => createSomeThirdPartyClient())
const reasons = injectWhy()
injectEffect(
() => {
const client = reasons.length
? createSomeThirdPartyClient()
: signal.get()
signal.set(client)
return () => client.destroy() // handle cleanup
},
[jwt] // rerun on jwt change
// `synchronous: true` no longer needed
)
return signal
}) It sounds like this is how I would set up the |
Beta Was this translation helpful? Give feedback.
-
As for why the entire I'm guessing that In the meantime, there is a workaround: Pass |
Beta Was this translation helpful? Give feedback.
-
I'm trying to instantiate instances of 3rd party libraries inside of atoms, and then expose the instance (using exports currently).
Is the below the best way to do this? I ran into issues with
useRef
, so I'm usinguseMemo
and then exporting the instance.One thing I ran into is that if I try to export the entire instance (vs specific functions), it seems to end up undefined when trying to use (see comments below).
This is with Zedux v2 rc.7.
Then in the component:
Beta Was this translation helpful? Give feedback.
All reactions