Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在循环内响应本机异步调用_Javascript_Reactjs_Asynchronous_React Native - Fatal编程技术网

Javascript 在循环内响应本机异步调用

Javascript 在循环内响应本机异步调用,javascript,reactjs,asynchronous,react-native,Javascript,Reactjs,Asynchronous,React Native,我在一个循环中有一个组件,它将在一个滑块中渲染,并带有一些位置图像。PlaceExtraInfo组件正在获取(http请求)关于每个位置的一些附加信息(用于传递的placeId),并假定将其显示在正确的位置幻灯片上: return places.map((place, index) => // there will be a fetch for each place id in the component <PlaceExtraInfo placeId={place.i

我在一个循环中有一个组件,它将在一个滑块中渲染,并带有一些位置图像。PlaceExtraInfo组件正在获取(http请求)关于每个位置的一些附加信息(用于传递的placeId),并假定将其显示在正确的位置幻灯片上:

return places.map((place, index) =>

   // there will be a fetch for each place id in the component
   <PlaceExtraInfo placeId={place.id} key={index} />
);
returnplaces.map((地点,索引)=>
//组件中的每个位置id都有一个提取
);
我正在使用Redux,在PlaceExtraInfo componentWillMount中,我正在调用操作来获取每个locationId,但是我被异步循环内陷阱(闭包)卡住了,因为在我的所有幻灯片中,最后都显示了最后一个http请求-响应结果


我知道我应该为每次迭代传递某种索引,但我不知道如何在这个获取迭代场景中使用它。

您可以尝试在这个组件中进行提取并向下传递,如下所示:

const placesInfo = await Promise.all(places.map(place =>
  fetch(url) //pass whatever you need from place into fetch
    .then((response) => <PlaceExtraInfo placeInfo={response} key={place.id} />)
)) 
//so now you will be making the api call and passing the info down to your component 
//and this will return an array of all the components
const placesInfo=wait Promise.all(places.map)(place=>
fetch(url)//将所需内容从place传递到fetch
.然后((响应)=>)
)) 
//现在,您将进行api调用,并将信息传递给您的组件
//这将返回所有组件的数组