Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using useCastChannel more than once doesn't work #316

Closed
prafulkadam opened this issue Apr 24, 2021 · 4 comments
Closed

using useCastChannel more than once doesn't work #316

prafulkadam opened this issue Apr 24, 2021 · 4 comments
Labels

Comments

@prafulkadam
Copy link

Hi,
If I try to cast from different screens then it stops working. Any solution for this?

@petrbela
Copy link
Contributor

Hmm I might need to change how the channels are registered... for now you might be able to solve it by only using the hook in a parent component and passing the channel as a prop to where you need it.

@petrbela petrbela added the bug label Jun 20, 2021
@prafulkadam
Copy link
Author

I need to keep pushing to same screen(with different result) which registers channel, hence passing props won't work unfortunately.

@ipnet-me
Copy link

Hello, any chance to fix this bug? Thank

@petrbela
Copy link
Contributor

petrbela commented Mar 12, 2022

After thinking about this a bit more, it can't be easily changed how it works without changing the API. Since useCastChannel sets up the underlying communication channel, you cannot call useCastChannel with the same namespace more than once (it would overwrite the attached listener). The channels are effectively global, and don't conform to React's component life-cycle.

If you're not able to lift the state up to a parent component, you can use the CastChannel directly (without the hook), and manage the channel yourself. In fact, managing the Cast state in a global store like Redux, Mobx or another out-of-tree state library would be a better approach anyway. For simplicity, this library provides hooks that store the state in the component tree, however, due to the nature of different in- and out-of-tree lifecycles, it doesn't work perfectly.

I'll mark it as won't fix and add a warning to the docs about the limitation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants