I have a piece of code that gets Metadata of a list of files from Firebase storage using the useEffect hook. I however, struggle to update my state within this useEffect. I have used the async/await pattern to mitigate the asynchronous nature of setState without much success.
My snippet in pseudocode:
const [someData,setSomeData]=useState([])
const dataArray=[]
useFocusEffect(
useCallback(()=>{
async function getData(){
const result=await listAll(storageRef);
await result.items.map(async (itemRef)=>{
const Metadata=await getMetadata(itemRef);
await dataArray.push({'name':Metadata.name})
await console.log(dataArray) //shows the array being populated on each iteration
})
await setSomeData(dataArray);
await console.log(someData) // I'm expecting it to print an updated state, prints an empty array instead
}
getData()
},[])
)
I also cannot seem to access the state in the return part of my code:
return(
{someData.map(({item})=>{
return(
<View><Text>{item.name}</Text><View>)})
outputs nothing as far as I can tell.
What am I missing in my approach?
Thanks in advance
2
Answers
as that function call is referring to the previous value
dataArray
is being re-initialized to[]
every render, and then you are settingsomeData
todataArray
, effectively settingsomeData
to an empty array. I think if you wrap your async map in a Promise.all then you can have dataArray scoped to yourgetData
function: