Iqbal Jan
Iqbal Jan

Reputation: 704

How do I use react context with react-navigation

I am trying to keep socket.io instance through all the app is mounted.

I am using react-native, socket.io and react-navigation.

So I need to react context, to keep my socket.io instance within all child component.

So kindly help me how can I use react context with react navigation, and also how can I update the context.

Upvotes: 2

Views: 1438

Answers (1)

user11469230
user11469230

Reputation:

that is so easy, just create a file example: ctx.js

then inside this file add the following code:

import React from 'react';
export const { Provider, Consumer } = React.createContext();

then in your root Component import Provide and put Provider as root Component like below:

 <Provider value={this.state}>
 </Provider>

then in any component you gonna use the context just import the Consumer like below:

class A extends Component{
 ....
}
const AComponent = props => (
    <Consumer>
        {context => <A {...props} context={context} /> }
    </Consumer>
);

Upvotes: 1

Related Questions