0

我在用户注册 ( index.js) 时使用AsyncStorage.setItem. 我正在尝试在Profile.js. 但是当我尝试打印会话变量(位置)时,什么也没有得到。以下是我的代码。我在做什么错?请帮忙

index.js

AsyncStorage.setItem('location').then((location) =>{
            this.setState({ location: location })
        })

Profile.js

fetch('http://loaclhost/apps/requirement.php',{
        method:'POST',
        headers: {
            'Accept':'application/json' ,
        },
        body: JSON.stringify({
            location:this.state.location,   
        })
    }).then( () =>
    {

        response => response.json();
    AsyncStorage.getItem('location').then((location) => {
        this.setState({location: location})
    })

    render(){
            return(
            <Container>
             <View>
            <TextInput 
        value={this.state.loctaion} />
             </View>
            </Container>
        );
    }
4

1 回答 1

0

这可能会帮助你,

index.js

AsyncStorage.setItem('location_key', 'location_value').then((location) =>{
    this.setState({ location: location })
})

Profile.js

var getLocation = function(){
    return AsyncStorage.getItem('location_key')
}

var fetchCall = function(location){
    return fetch('http://loaclhost/apps/requirement.php',{
        method:'POST',
        headers: {
            'Accept':'application/json' ,
        },
        body: JSON.stringify({
            location: location, //Resolved location
        })
    })
}


function fetchRequest(){

    getLocation()
    .then(fetchCall)
    .then((response) => {
        //Response handling
    })
}
于 2017-09-07T08:23:14.000 回答