React Native在fetch结束后执行一个函数

时间:2016-08-05 11:26:00

标签: javascript function reactjs native fetch

我遇到的问题是,当我更改页面时,获取仍未结束提取,因此某些变量为空。我需要它,直到完成提取功能,他没有调用下一个函数。以下是冲突的两个函数:



  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作为时间解决方案的原因。

0 个答案:

没有答案