React native 重定向到另一页之前的异步存储

React native 重定向到另一页之前的异步存储,react-native,async-await,React Native,Async Await,我正在尝试创建一个登录页面,其中userId将通过asyncstorage存储,并将在重定向页面上获取该值,并将其放置在一个隐藏的attibute中,以便在另一次单击按钮时使用 这是我的TouchableOpacity登录页面,它有一个onPress功能 <TouchableOpacity style={styles.userBtn} onPress={this._login} ><Text style={styles.btnText}>Log In&

我正在尝试创建一个登录页面,其中userId将通过asyncstorage存储,并将在重定向页面上获取该值,并将其放置在一个隐藏的attibute中,以便在另一次单击按钮时使用

这是我的TouchableOpacity登录页面,它有一个onPress功能

<TouchableOpacity 
    style={styles.userBtn}
    onPress={this._login}
 ><Text style={styles.btnText}>Log In</Text>
 </TouchableOpacity>
下面是我获取authID的详细信息页面函数

这是在render()之前

这是在render()之后

所以基本上在render()部分之前,在构造函数中,我调用了render()部分之后的函数。但它总是向我返回一个空警报


对不起,我才开始学英语一个星期。我正在练习我的技能,所以如果我解释的方式和询问的方式有问题,我向您道歉。

您是否尝试将_getUserId放入componentDidMount方法中?有时最好调用componentDidMount中的所有函数,该函数需要在呈现组件后执行。还可以尝试登录以检查您的身份验证id是否存在问题,因为它可以为空。如果isLoggedIn为1,则您的身份验证id存在问题

例如

class ABC extends Component {
constructor(props){
super(props);
this.state={currentTime: null, currentDay: null, longitude: 0, latitude: 0, error: null }
this.daysArray = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

}

async componentDidMount(){
let result = await this._getUserId();
}


_getUserId = async() => {
  const userId = await AsyncStorage.getItem('authId');
  const isLoggedIn = await AsyncStorage.getItem('isLoggedIn');
  alert(userId,isLoggedIn);
}


render(){
}
}

您是否尝试将_getUserId放入componentDidMount方法中?有时最好调用componentDidMount中的所有函数,该函数需要在呈现组件后执行。还可以尝试登录以检查您的身份验证id是否存在问题,因为它可以为空。如果isLoggedIn为1,则您的身份验证id存在问题

例如

class ABC extends Component {
constructor(props){
super(props);
this.state={currentTime: null, currentDay: null, longitude: 0, latitude: 0, error: null }
this.daysArray = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

}

async componentDidMount(){
let result = await this._getUserId();
}


_getUserId = async() => {
  const userId = await AsyncStorage.getItem('authId');
  const isLoggedIn = await AsyncStorage.getItem('isLoggedIn');
  alert(userId,isLoggedIn);
}


render(){
}
}

AsyncStorage保存数据是异步的,因此保存的数据不会立即保存。另一方面,获取数据方法应该放在componenttdDidMount或navigation方法上。在这些方法中,UI已经呈现。同时,您保存的数据应该解析为JSON,并且应该很小,这样可以减少时间

componentDidMount() {
    AsyncStorage.getItem('key', (error, result) => {        
        if (error) {
           //do something
        } else {
            //parse the data
        }
    });
}

AsyncStorage保存数据是异步的,因此保存的数据不会立即保存。另一方面,获取数据方法应该放在componenttdDidMount或navigation方法上。在这些方法中,UI已经呈现。同时,您保存的数据应该解析为JSON,并且应该很小,这样可以减少时间

componentDidMount() {
    AsyncStorage.getItem('key', (error, result) => {        
        if (error) {
           //do something
        } else {
            //parse the data
        }
    });
}

我试过了,但仍然为空,当我试过isLoggedIn时,它返回1,这意味着异步正在工作,并且存储的authId存在问题。您存储的身份验证id的数据类型是什么?我使用了Lenoarod的答案,但也应用了您告诉我的关于数据类型的Gauvar。当我使用这个JSON.stringify(responseJson['authId'])时,它现在显示了正确的数据。谢谢大家的帮助。我试过了,但仍然是空的,当我试过isLoggedIn时,它返回1这意味着,您的异步正在工作,并且您存储的authId存在问题。您存储的身份验证id的数据类型是什么?我使用了Lenoarod的答案,但也应用了您告诉我的关于数据类型的Gauvar。当我使用这个JSON.stringify(responseJson['authId'])时,它现在显示了正确的数据。谢谢大家的帮助。
componentDidMount() {
    AsyncStorage.getItem('key', (error, result) => {        
        if (error) {
           //do something
        } else {
            //parse the data
        }
    });
}