Fetching Data with Jotai
Explore how to fetch and manage real-time data in React Native applications using Jotai. Understand creating asynchronous atoms for server data fetching and how to integrate read-only, write-only, and read-write atoms to manage and update state effectively within your app.
Fetching real-time data
We successfully set up mocked image data in our app, but we would like to fetch real data from the server. Going back to the Jotai docs, we will find a guide on asynchronous atoms. Here’s what our async atom for fetching images will look like:
Let's look at the details of the code:
Line 2: We add a
requestBaseimport to use URLs more comfortably.Line 9: Then, we proceed to create a basic atom
urlAtowith the specific URL.Lines 12–16: We can see that the last function is the
asyncatom. We know it’s async because it uses theasynckeyword. The body of theasyncatom function is afetchfunction and data return. ...