我遇到的问题是,当我更改页面时,获取仍未结束提取,因此某些变量为空。我需要它,直到完成提取功能,他没有调用下一个函数。以下是冲突的两个函数:
componentDidMount() {
this.fetchData();
setTimeout(() => {
InteractionManager.runAfterInteractions(() => {
this.setState({renderPlaceholderOnly: true});
});
}, 300);
}
fetchData() {
return fetch(REQUEST_URL+this.props.boat.id)
.then((response) => response.json())
.then((responseData) => {
this.setState({
boat2: responseData,
loaded: true,
name:responseData.name,
maker:responseData.maker,
model:responseData.model,
buildYear:responseData.buildYear,
refitYear:responseData.refitYear,
boatType:responseData.boatType,
withSkipper:responseData.withSkipper,
licenseNeeded:responseData.licenseNeeded,
places:responseData.places,
berths:responseData.berths,
cabins:responseData.cabins,
toilets:responseData.toilets,
length:responseData.length,
beam:responseData.beam,
draft:responseData.draft,
motorType:responseData.motorType,
power:responseData.power,
maxSpeed:responseData.maxSpeed,
cruiseSpeed:responseData.cruiseSpeed,
consumption:responseData.consumption,
water:responseData.water,
tank:responseData.tank,
IMGS : [
'https://images.unsplash.com/photo-1441742917377-57f78ee0e582?h=1024',
'https://images.unsplash.com/photo-1441716844725-09cedc13a4e7?h=1024',
'https://images.unsplash.com/photo-1441448770220-76743f9e6af6?h=1024'
],
});
})
.catch((error) => alert(error) )
.done();
}

在componentDidMount
执行提取但是在提取结束之前执行interactionManager
,这就是我使用setTimeout
作为时间解决方案的原因。