reactjs - AsyncStorage getItem 无法在具有反应 native 路由器通量的抽屉内工作

标签 reactjs react-native react-native-router-flux asyncstorage

我正在尝试获取抽屉组件中 AsyncStorage 内的值。我已经完成了条件渲染以显示 loginlogout 按钮在抽屉内。因此,成功登录后,如果 AsynStorage 内有值,抽屉内的按钮应为 Logout 而不是 Login。但最初是在登录后当抽屉打开时,AsyncStorage的值没有获取。只有当我单击抽屉中的特定项目时,它才会获取。那么当抽屉打开时,如何获取抽屉内的值?我已经完成了以下内容

login.js

async saveItem(item, selectedValue) {
    try {
      await AsyncStorage.setItem(item, selectedValue);
    } catch (error) {
      console.error('AsyncStorage error: ' + error.message);
    }
  }

...
....
...
.then((response) => response.json())
      .then((responseData) => {
        this.setState({
          login: responseData
        })
        if (responseData.status == true) {

          this.saveItem('userdetail', responseData.token.access_token);
          this.saveItem('userimage', responseData.user.avatar);
          this.saveItem('user', responseData.user.name);
          Actions.dashBoard();
          this.setState({ isLoading: false });

        } else {
....
}

抽屉.js

componentDidMount() {
        AsyncStorage.getItem("user").then(value => {
            this.setState({ name: value })
        })
        console.log(this.state.name);
    }

onPressList = (data, index) => {
        AsyncStorage.getItem("user").then(value => {
            this.setState({ name: value })
        })
        if (this.state.name) {
            if (data.route == "profile")
                Actions.profile();
       ...
      ....
      ...
}
}

render(){
return(
{this.state.name? (
 <ListItem
                            button
                            noBorder
                            onPress={() => this.onPressLogout()}
                        >

                            <Text style={[styles.text, { textAlign: 'left' }]}>
                                Logout
                            </Text>

                        </ListItem>
) : (
 <ListItem
                            button
                            noBorder
                            onPress={() => this.onPressLogin()}
                        >
                            <Text style={[styles.text, { textAlign: 'left' }]}>
                                Login
                            </Text>

                        </ListItem>) }
)
}     

在抽屉内的每个列表项中,我检查是否设置了 name 。仅当 name 存在时它才会导航。但问题最初出现在 componentDidMount 我没有在 AsyncStorage 中获取值。请帮我找到解决方案。问题仅存在于侧栏中。我在 AsyncStorage< 中获取值 在所有其他屏幕中。请帮忙。任何帮助将非常感激。谢谢。

最佳答案

问题是您在将用户添加到异步存储之前检查用户。当您单击某个项目时它起作用的原因是因为您每次单击列表项时都会检查用户。您可以使用 componentWillUpdate 代替 componentDidMount。我猜测当您单击抽屉时,它会更新组件,然后检查用户(因为 React 重新渲染整个组件子树)。

希望有帮助

    componentWillUpdate() {
        AsyncStorage.getItem("user").then(value => {
            this.setState({ name: value })
        })
        console.log(this.state.name);
    }

关于reactjs - AsyncStorage getItem 无法在具有反应 native 路由器通量的抽屉内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54707452/

相关文章:

javascript - 使用 react-native-router-flux 与 nativebase

javascript - setState在react中不接受动态值?

reactjs - Redux 将对象添加到数组

reactjs - WebStorm 中的 "Invalid number of arguments, expected 0"

react-native - TextInput 中的大写文本 - react native

javascript - react native : How to individually control each onpress() from map() generated components

android - react-native-router-flux 禁用 android 后退按钮返回登录屏幕

javascript - 根据发出警告的属性传递属性

react-native - Share 在 iOS 13 上不起作用。 Expo 模块

react-native-router-flux:如何防止在选项卡之间切换时重置选项卡场景历史堆栈?