How should the new context api work with React Native navigator?

后端 未结 4 778
野性不改
野性不改 2021-01-30 18:37

I created a multiscreen app using React Navigator following this example:

import {
  createStackNavigator,
} from \'react-navigation\';

const App = createStackN         


        
相关标签:
4条回答
  • 2021-01-30 19:00

    https://wix.github.io/react-native-navigation/docs/third-party-react-context/

    As RNN screens are not part of the same component tree, updating the values in the shared context does not trigger a re-render across all screens. However you can still use the React.Context per RNN screen component tree.

    If you need to trigger a re-render across all screens, there are many popular third party libraries such as MobX or Redux.

    0 讨论(0)
  • 2021-01-30 19:04

    This answer takes in consideration react-navigation package.

    You have to wrap your App component with the ContextProvider in order to have access to your context on both screens.

        import { createAppContainer } from 'react-navigation'
        import { createStackNavigator } from 'react-navigation-stack'
        import ProfileContextProvider from '../some/path/ProfileContextProvider'
    
        const RootStack = createStackNavigator({
          Home: { screen: HomeScreen },
          Profile: { screen: ProfileScreen },
        });
    
        const AppContainer = createAppContainer(RootStack)    
        const App = () => {
          return (
          <ProfileContextProvider>
            <AppContainer />
          </ProfileContextProvider>);
        }
    
    0 讨论(0)
  • 2021-01-30 19:07

    You can make it like this.

    Create new file: GlobalContext.js

    import React from 'react';
    
    const GlobalContext = React.createContext({});
    
    export class GlobalContextProvider extends React.Component {
      state = {
        isOnline: true
      }
    
      switchToOnline = () => {
        this.setState({ isOnline: true });
      }
    
      switchToOffline = () => {
        this.setState({ isOnline: false });
      }
    
      render () {
        return (
          <GlobalContext.Provider
            value={{
              ...this.state,
              switchToOnline: this.switchToOnline,
              switchToOffline: this.switchToOffline
            }}
          >
            {this.props.children}
          </GlobalContext.Provider>
        )
      }
    }
    
    // create the consumer as higher order component
    export const withGlobalContext = ChildComponent => props => (
      <GlobalContext.Consumer>
        {
          context => <ChildComponent {...props} global={context}  />
        }
      </GlobalContext.Consumer>
    );
    

    On index.js wrap your root component with context provider component.

    <GlobalContextProvider>
      <App />
    </GlobalContextProvider>
    

    Then on your screen HomeScreen.js use the consumer component like this.

    import React from 'react';
    import { View, Text } from 'react-native';
    import { withGlobalContext } from './GlobalContext';
    
    class HomeScreen extends React.Component {
      render () {
        return (
          <View>
            <Text>Is online: {this.props.global.isOnline}</Text>
          </View>
        )
      }
    }
    
    export default withGlobalContext(HomeScreen);
    

    You can also create multiple context provider to separate your concerns, and use the HOC consumer on the screen you want.

    0 讨论(0)
  • 2021-01-30 19:09

    If you want the detailed tutorial you could follow the below link : Visit : https://www.thelearninguy.com/simple-react-native-context-api-detailed

    A very long answer would be as follows.

    import React, {Component} from 'react';
    import {Text, View, Button} from 'react-native';
    
    //make a new context
    const MyContext = React.createContext();
    
    //create provider component
    class MyProvider extends Component {
        state = {
            name: "The Learnin Guy",
            age: 50
        };
        increaseAge = () => {
            this.setState({
                age: this.state.age + 1
            });
        };
    
        render() {
            return (
                <MyContext.Provider
                    value={{
                        state: this.state,
                        increaseAge: this.increaseAge
                    }}
                >
                    {this.props.children}
                </MyContext.Provider>
            );
        }
    }
    
    class Person extends Component {
        render() {
            return (
                <View>
                    <Text>This is Person Component</Text>
                    <MyContext.Consumer>
                        {(context) => (
                            <React.Fragment>
                                <Text>Name: {context.state.age}</Text>
                                <Text>Age: {context.state.age}</Text>
                                <Button title="IncreaseAge" onPress={context.increaseAge} />
                            </React.Fragment>
                        )}
                    </MyContext.Consumer>
                </View>
            );
        }
    }
    
    class Family extends Component {
        render() {
            return (
                <View>
                    <Text>This is Family Component</Text>
                    <MyContext.Consumer>
                        {(context) => (
                            <React.Fragment>
                                <Text>Age: {context.state.age}</Text>
                            </React.Fragment>
                        )}
                    </MyContext.Consumer>
                    <Person/>
                </View>
            );
        }
    }
    
    class App extends Component {
        render() {
            return (
                <MyProvider>
                    <View>
                        <Text>This is App Component</Text>
                        <MyContext.Consumer>
                            {(context) => (
                                <React.Fragment>
                                    <Text>Age: {context.state.age}</Text>
                                </React.Fragment>
                            )}
                        </MyContext.Consumer>
                        <Family/>
                    </View>
                </MyProvider>
            );
        }
    }
    
    export default App;
    

    Courtesy - https://www.thelearninguy.com

    0 讨论(0)
提交回复
热议问题