首页 > 解决方案 > 为每个页面设置一个状态变量

问题描述

在每一页上,我都执行此功能:

import * as sync_helper from '../utils/sync';

componentWillUnmount() { this._isMounted = false; }
componentDidMount() {

  this._isMounted = true;

  sync_helper.getLocal().then((l) => {

    if(this._isMounted)
      this.setState({ current_user: l.current_user,loaded: true });

  });
}

我有时也有更多的异步函数,componentDidMount()但这个在每一页中。

有没有办法自动将我添加current_user到每个状态而不将其放在每个页面上?

我正在使用反应导航(堆栈)

标签: javascriptreact-nativereact-native-navigationreact-native-navigation-v2

解决方案


推荐阅读