Fetching Data
Learn how to fetch asynchronous data and optimizing with generator functions in MobX-State-Tree.
We'll cover the following...
We'll cover the following...
We have our image list stored on the server. MobX-State-Tree proposes two ways of fetching asynchronous data, but both are actions.
Creating an action
Let’s create an action in the store:
We need an asynchronous function that will do the fetching—we have called it fetchImages on lines 14–22 above. This function uses JavaScript’s fetch function on line 16 and returns data from the server on line 22.
Passing data into the model
Now that we have the data, we need to pass it to the LikedImages model. Let’s add a function that will do just that:
Here’s an explanation of the above code: