0
const renderPicCards=()=>{
    return albums.assets.map(element => {  
      AsyncStorage.getItem(element.filename).then((result)=>{
        return(    
          <PicCard imageUri={element.uri} key={element.id} Temperature={result.Temperature} />
          )
      })
        .catch((err)=>{console.log('error is',err)})  
    
})

}

here the return statement is not working; asyncstorage will result in a promise..what am i doing wrong here? i basically want a list of cards whose one prop is being fetched fro asyncstorage library

  • Does this answer your question? [Using promise function inside Javascript Array map](https://stackoverflow.com/questions/39452083/using-promise-function-inside-javascript-array-map) – Brett East Mar 06 '21 at 08:20
  • You can use the promise all. – Palash Kanti Bachar Mar 06 '21 at 08:30
  • @navaneeth001 I think you should wrap code inside map in promise or you can use async await. – Vikas Mar 06 '21 at 08:33
  • 2
    I don't think you can render anything asynchronously, with or without a loop. You need to fetch the data, store it in the state and render components based on the state – Nadia Chibrikova Mar 06 '21 at 08:39

3 Answers3

0

I believe adding a return before AsyncStorage will solve your problem.

return albums.assets.map(element => {  
  return AsyncStorage.getItem(element.filename).then((result)=>{
    return(    
      <PicCard imageUri={element.uri} key={element.id} Temperature={result.Temperature} />
      )
  })
    .catch((err)=>{console.log('error is',err)})  
Antonis S
  • 733
  • 6
  • 15
0

You should async/await response from AsyncStorage.

const renderPicCards = () => {
   return albums.assets.map( async (element) => {
      try {
         const result = await AsyncStorage.getItem(element.filename);
         return (<PicCard imageUri={element.uri} key={element.id} Temperature={result.Temperature} />);
      } catch (err) {
         console.log('error is', err);
      }
   });
};
0

i solved this problem by fetching the async data inside the subcomponent piCard instaed of in the main component like this and it works fine..Thanks :)

`function picCard(props) {
const [details,setdetails]=useState(null);
useEffect(() => {
  AsyncStorage.getItem(props.filename).then((result)=>{
    setdetails(JSON.parse(result))
  }).catch((err)=>{console.log('error is',err)});
}, [])
...more code