如何使用 React Native 在会话中存储值?
How to store values in session using React Native?
我使用 AsyncStorage.setItem
在用户注册 (index.js
) 时将位置设置为会话变量。我正在尝试访问 Profile.js
中的这个会话变量。但是,当我尝试打印会话变量(位置)时,什么都没有 getting.Following 是我的 code.What 错了吗?请帮忙
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>
);
}
这可能对你有帮助,
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
})
}
我使用 AsyncStorage.setItem
在用户注册 (index.js
) 时将位置设置为会话变量。我正在尝试访问 Profile.js
中的这个会话变量。但是,当我尝试打印会话变量(位置)时,什么都没有 getting.Following 是我的 code.What 错了吗?请帮忙
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>
);
}
这可能对你有帮助,
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
})
}