Skip to content

How to use subscribe like useCallback ? #235

Answered by voliva
StringKe asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @StringKe

For your specific question, the observable returned from createSignal is just a plain rxjs Observable. React-rxjs doesn't currently have any function to directly subscribe to an observable because it doesn't directly help with managing state. But you can use to any observable in your component with just a useEffect:

const [singal$, emitSignal] = createSignal();

function MyComponent() {
  useEffect(() => {
    // Subscribe to the signal
    const sub = signal.subscribe((event) => {...});
    // Unsubscribe on cleanup
    return () => sub.unsubscribe();
  }, []);

  // ...
}

However, I'd like to know a bit more about the specific use case so I can help you better. If it does …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by StringKe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants