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

useChannel(false) to prevent eager channel connection #44

Open
alex-cory opened this issue Feb 16, 2021 · 4 comments
Open

useChannel(false) to prevent eager channel connection #44

alex-cory opened this issue Feb 16, 2021 · 4 comments

Comments

@alex-cory
Copy link
Contributor

alex-cory commented Feb 16, 2021

Sometimes you don't want to subscribe to a channel until another bit of data is available. It would be nice if you allowed useChannel to accept undefined, null, or false as a conditional to wait to subscribe. Similar to how useSWR does it.
For instance:

const Messenger = () => {
  const my = useMe()
  const channelURL = my.username && `/api/messages/${my.username}`
  const channel = useChannel(channelURL)
  useEvent(channel, eventType, data => {
  })
}

In the above example, this prevents sending a subscription request from happening until we get my.username. Otherwise we would be subscribing to channel /api/messages/undefined

@alex-cory
Copy link
Contributor Author

Actually, this works, but it would be nice to not have the warning. I know it helps people if they haven't added a channel, but sometimes you don't want to add a channel.

@mayteio
Copy link
Owner

mayteio commented Feb 17, 2021

All good - I love it. Keen to add it in. Did you want to try a PR? Otherwise I can get to it later in the week.

@mayteio
Copy link
Owner

mayteio commented Feb 17, 2021

We’ve got two options - anything that evaluates to false doesn’t attempt to subscribe OR explicitly pass null to prevent subscription. This might be safer so that it still warns about passing undefined if people are generating channel names with an expression - similar to SWR as you mentioned.

@alex-cory
Copy link
Contributor Author

I personally like the false better. Then you don't have to do const url = bool ? 'url' : null you can just do const url = bool && 'url'. Cleaner in my opinion.

@mayteio mayteio changed the title Feature Request useChannel useChannel(false) to prevent eager channel connection Mar 2, 2021
mayteio added a commit that referenced this issue Aug 11, 2022
Some improvements...

#89 Incompatible with React versions >16
#40 is this hooks automatically disconnect channels?
#43 Introduce to hoist channel connection
#44 useChannel(false) to prevent eager channel connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants