Global state in React Native

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

    If you are new to react (as me) and got confused by the first answer. First, use a component Class

    export default class App extends React.Component {
    
    constructor(props) {
      super(props);
      this.state = {
        walk: true
      };
      GLOBAL.screen1 = this;
    }
    
    render() {
      return (
        
          
          {this.state.walk ? (
            <>
              
            
          ) : (
            
          )}
        
        
      
     )
    }
    

    Then you can do in any other component (My components are on /components, global is on root):

    import GLOBAL from '../global.js'
    
    GLOBAL.screen1.setState({walk:false})
    

提交回复
热议问题