javascript - 使用 AsyncStorage 更新状态 - React Native

标签 javascript react-native

当我想要更新组件的状态时,我遇到了 componentWillMount 的一个小问题,我收到此错误。

enter image description here

代码是这样的:

export default class RouterComponent extends Component {

constructor(props) {
    super(props);
    this.state = {
        isLogin: null
    }
}

componentWillMount = async () => {
    AsyncStorage.getItem('@MyUserFireBase:key').then(response => {
        this.setState({ 'isLogin': true });
    }).done();
}



render() {

    return (
        <Router>
            <Stack key="root" hideNavBar>
                <Stack key="main">
                    <Scene key="login" initial={!this.state.isLogin} component={LoginForm} title="Login" />
                    <Scene key="employeeList" initial={this.state.isLogin} component={EmployeeList} title="Employee List" />
                </Stack>
            </Stack>
        </Router >
    )
}

}

我花了 3 天多的时间寻找解决方案,但找不到。我究竟做错了什么? 感谢您的宝贵时间

最佳答案

查看组件生命周期方法来帮助您解决问题:https://engineering.musefind.com/react-lifecycle-methods-how-and-when-to-use-them-2111a1b692b1

对于我的答案,请尝试以下操作:

componentDidMount = async () => {
    AsyncStorage.getItem('@MyUserFireBase:key').then(response => {
        this.setState({ 'isLogin': true });
    }).done();
}

此外,如果这不起作用,请尝试更新,因为您正在设置状态:

componentDidUpdate() =  {
        AsyncStorage.getItem('@MyUserFireBase:key').then(response => {
            this.setState({ 'isLogin': true });
        }).done();
    }

关于javascript - 使用 AsyncStorage 更新状态 - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50412863/

相关文章:

javascript - 从外部 js 文件触发 js 事件,如 onclick

javascript - CSS 按钮图像 :hover

javascript - 下拉菜单选择的选项与其他字体不同

javascript - dojox/图表/Chart2D : how to format the label of minor tick?

javascript - 在 React Native 中使用外部 API 呈现 Flatlist 的正确方法

javascript - react native : Getting the position of an element

javascript - Handlebars 查找以生成 href

javascript - 将页面导入到 App.js 时,它显示为空白

javascript - 为什么 Pod 依赖管理器没有检测到安装所需的模块?

javascript - 无法从回调 RefreshControl 启动刷新操作