您面临的问题是两种方法都是async
. 在您的情况下,您应该getData
在获取项目后调用 to 作为回调。
用你的代码解释:
componentWillMount(){
AsyncStorage.getItem('key').then((codigo)=>{
//This code runs async, so when you call getData, value has not been changed yet (Or at least you cannot be sure).
this.setState({value: JSON.parse(codigo)});
//Printing here this.state.value will help you to understand
this.getData()
})
}
getData(){
fetch(`URL/portalacv_ws.asmx/GetDetalhesViatura?CarID=${this.state.value}`)
.then((response) => { return response.json()})
.then(res => {
this.setState({data: res})
})
}
如何解决?:
componentWillMount(){
AsyncStorage.getItem('key').then((codigo)=>{
this.setState({value: JSON.parse(codigo)}, () => {
//Here you are pretty sure that the setState has already done.
this.getData()
});
})
}
getData(){
fetch(`URL/portalacv_ws.asmx/GetDetalhesViatura?CarID=${this.state.value}`)
.then((response) => { return response.json()})
.then(res => {
this.setState({data: res})
})
}
编辑:
在查看了整个组件之后,得出的结论是 render 方法在 setState 之前和之后执行了一次,这就是为什么你第一次有 undefined ,第二次是你期望的值。
因此,为了解决这种情况,一种可能的方法是标记获取数据的操作并在获取完成后进行渲染。或多或少,这个想法是:
export default class Favoritos extends Component {
constructor(props) {
super(props);
this.state = {
value: null,
data: null,
fetching: false
};
//Binding is not needed, but...
this.getData = this.getData.bind(this);
this.onPress = this.onPress.bind(this);
}
componentWillMount(){
this.setState({ fetching: true }, () => {
AsyncStorage.getItem('key').then((codigo)=>{
this.setState({value: JSON.parse(codigo)}, () => {
this.getData()
.then((data) => {
this.setState({
data: data,
fetching: false
})
})
});
})
});
}
getData(){
return fetch(`URL/portalacv_ws.asmx/GetDetalhesViatura?CarID=${this.state.value}`)
.then((response) => { return response.json()})
}
onPress(){
this.setState({ fetching: true }, () => {
this.getData()
.then((data) => {
this.setState({
data: data,
fetching: false
})
})
});
}
render() {
if(this.state.fethcing){
return (
<View style={{ flex: 1, backgroundColor: 'white' }}>
Fetching data...
</View>
);
} else {
return (
<View style={{ flex: 1, backgroundColor: 'white' }}>
<ScrollView>
<TouchableHighlight onPress={this.onPress}>
...
</TouchableHighlight>
<Text>
{this.state.value}
</Text>
</ScrollView>
</View>
);
}
}
}
在上面的代码中,我只留下了值得质疑的代码,原来的代码要多得多。