简体   繁体   中英

use AsyncStorage inside a _onRefresh?

How is it possible to pull information from AsyncStorage from a _onRefresh ?

Here is what i'm trying:

_onRefresh = () => {
        const token = await AsyncStorage.getItem('access'); // I cannot do this (I know)
        const access = 'Bearer ' + token;
        this.setState({ refreshing: true });
        axios.get(`http://laundry.test/api/auth/main`, {
            headers: {
                'Authorization': access,
            }
        }).then(res => {
            this.setState({ laundries: res.data.laundries });
            this.setState({ refreshing: false });
        })
    }

how can I perform what I'm trying to do? I need to pull the token to access to my API and grab what I need, without authentication... I won't be pulling anything.

Make your _onRefresh as async to solve your issue.

_onRefresh = async () => {
  const token = await AsyncStorage.getItem("access");
  const access = "Bearer " + token;
  this.setState({ refreshing: true });
  let response = axios.get(`http://laundry.test/api/auth/main`, {
    headers: {
      Authorization: access,
    },
  });
  this.setState({
    laundries: response.data.laundries,
    refreshing: false,
  });
};

Hope this helps you. Feel free for doubts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM