useChannel(false) to prevent eager channel connection
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
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.
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.
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.
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.