Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Do not subscribe if channel empty string #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cmcaboy
Copy link

@cmcaboy cmcaboy commented Dec 29, 2021

I was thinking of some way to conditionally subscribe, but the hook does not allow for that. As a work-around, I would like for the useChannel hook to subscribe only when the channel name is a truthy value.

@thisisjofrank
Copy link
Contributor

Would it be better to use an if condition around the call to useChannel? Otherwise we'd have to return something valid from the useChannel call that wouldn't make semantic sense.

@michelts
Copy link

Would it be better to use an if condition around the call to useChannel

I don't think this is possible, you would break the rules of hooks.

My specific use-case is to lock usePresence for users behind a paywall. I wouldn't want to even spend connections with invalid users.

For now, I'm planning to write a component that conditionally render other components. Those would call useChannel/usePresence or stub them:

const App = () => (
  <MyChannelProvider>
     <Component />
  </MyChannelProvider>
)

const MyChannelProvider = () => {
  if(isValidUser) {
    return <ActualChannelProvider />
  } else {
    return <StubbedChannelProvider />
  }
}

const Component = () => {
  const channelData = useMyChannelProviderContext();
}

But it is a lot of boilerplate code. Having a way to skip the hook would be nice (similar to how we can conditionally fetch data with SWR, or skip Apollo requests).

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

Successfully merging this pull request may close these issues.

3 participants