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

Bind Connection using usePusher | React native #19

Closed
juandl opened this issue Aug 31, 2020 · 7 comments
Closed

Bind Connection using usePusher | React native #19

juandl opened this issue Aug 31, 2020 · 7 comments

Comments

@juandl
Copy link

juandl commented Aug 31, 2020

Hi, I'm trying to get the status of the connection. using

 const { client } = usePusher();

 useEffect(() => {
    client.connection.bind('connected', () => {
      console.log('Realtime is go!');
    });
  }, []);

but it's not working, any way to do this?

@juandl
Copy link
Author

juandl commented Aug 31, 2020

I also try to use

const { client } = usePusher();

useEvent(client.connection, 'connected', (data) =>  console.log('Realtime is go!'));

but it's not working too.

@mayteio
Copy link
Owner

mayteio commented Aug 31, 2020

Hey, first thing I notice is that client is missing from your dependancy array. On the first render, client will not be defined. Since client is stored in state though, when it becomes available it'll re-trigger the effect:

const [client, setClient] = useState<any | undefined>();

If it still doesn't work, please provide a codesandbox and I can have a look!

@juandl juandl changed the title Bind Connection using usePusher Bind Connection using usePusher | React native Aug 31, 2020
@juandl
Copy link
Author

juandl commented Aug 31, 2020

@mayteio you are right, i wait for the "client" and is working. But in react native it's not working.

@mayteio
Copy link
Owner

mayteio commented Aug 31, 2020

That's great. Have you checked the rn example? https://github.com/mayteio/use-pusher/tree/master/examples/native-use-pusher-example

@juandl
Copy link
Author

juandl commented Aug 31, 2020

@mayteio yes I do, but it's not working, I try with
-React navigation x5
-Expo (latest version)

I wrap the Provider with the navigation.. all the hooks work but in the case of getting the status of the connection, its not working

 const { client } = usePusher();

 useEffect(() => {
 if(client){
   client.connection.bind('connected', () => {
      console.log('Realtime is go!');
    });
  }
}, [client]);

@juandl
Copy link
Author

juandl commented Aug 31, 2020

Hi @mayteio , I found the solution, apparently passing the provider like this ist not working...

const Navigation = () => {
  return (
      <NavigationContainer>
        <Stack.Navigator'>
          <Stack.Screen name='Home' component={HomePage} />
        </Stack.Navigator>
      </NavigationContainer>
  );
};
export default function App() {
  return (
   <PusherProvider {...config}>
      <Navigation />
   </PusherProvider>
  );
}

But passing directly to the NavigationContainer it's working:

const Navigation = () => {
    <PusherProvider {...configPusher}>
      <NavigationContainer>
        <Stack.Navigator>
          <Stack.Screen name='Home' component={HomePage} />
        </Stack.Navigator>
      </NavigationContainer>
    </PusherProvider>
};

just leaving here for someone else :)

@juandl juandl closed this as completed Aug 31, 2020
@mayteio
Copy link
Owner

mayteio commented Sep 1, 2020

Hey @juandl,

That's odd! Glad you could figure it out, cheers.

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