Global state in React Native

前端 未结 6 1691
自闭症患者
自闭症患者 2020-12-06 10:03

I am developing a React Native application.

I want to save the user id of the person who is logged in and then check if the user is logged in in every single compone

6条回答
  •  时光取名叫无心
    2020-12-06 10:43

    I usually create a global.js containing:

    module.exports = {
       screen1: null,
    };
    

    And get the value of the state on the screen

    import GLOBAL from './global.js'
    
    constructor() {
    
        GLOBAL.screen1 = this;
    
    }
    

    Now you can use it anywhere like so:

    GLOBAL.screen1.setState({
        var: value
    });
    

提交回复
热议问题