Global state in React Native

前端 未结 6 1692
自闭症患者
自闭症患者 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:52

    I usually do globals like this:

    I creat an globals.js

    module.exports = {
      USERNAME: '',
    };
    

    Something like that to store the username then you just need to import :

    GLOBAL = require('./globals');
    

    And if you wanna store the Data, lets say you want to save the username just do :

    var username = 'test';
    GLOBAL.USERNAME = username;
    

    And there you go , you just need to import GLOBAL on the pages you want and use it, just use if (GLOBAL.username == 'teste').

提交回复
热议问题