Image following object structure:
const countries = [
{
regions: [
{
cities:[
{
temperature: 50
},
...
]
}
...
]
},
...
]
Now every object has a corresponding component: <Country/>
, <Region/>
, <City/>
.
The state is stored and managed in a context. You can click on a City to refresh it's current temperature (API Call). You can also click on a floating action button to update all cities.
Question
How can I trigger a refresh on each city with the floating action button, considering that every city is loading itself (for UX reasons) and show a loading animation until they are done loading. If I just get the whole object from the API again with all countries, regions and cities, I have no control over when which table is finished and can't show an animation accordingly.
Best solutions i could think of
Create an event emitter kinda thing, and trigger each city onRefreshClicked
. Or create a subscriber functionality in which you can pass a callback, like you do with web sockets.
But feel like my solutions are an anti-pattern and against the one-way data flow
rule of react. Als it feels really dirty. Any ideas?
I am using react-hooks btw.