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

后端 未结 4 780
野性不改
野性不改 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: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 (
                
                    {this.props.children}
                
            );
        }
    }
    
    class Person extends Component {
        render() {
            return (
                
                    This is Person Component
                    
                        {(context) => (
                            
                                Name: {context.state.age}
                                Age: {context.state.age}
                                

    Courtesy - https://www.thelearninguy.com

提交回复
热议问题