avatarTrevor-Indrek Lasn

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2924

Abstract

figcaption></figure><p id="3b95">This might seem a bit confusing. We have a function, which returns a function? Yep. This is Thunk doing its magic.</p><p id="45d5">Think about this for a second. When requesting data, there are 3 states.</p><ol><li>Requesting the data.</li><li>Data Requesting Successful <code>(200 — got the data!)</code></li><li>Data Requesting Fail</li></ol><p id="3055">This makes perfect sense. Think it of as a promise, there’s <b><i>calling</i></b> the Promise, r<b><i>esolving</i></b> the promise and <b><i>ejecting</i></b> the promise.</p><p id="e1f8">Let’s finish implementing the rest of the thunk action. Try to implement fetching the data.</p><figure id="c412"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MPH-f6JA828Hw3Fp1N3P2g.png"><figcaption></figcaption></figure><p id="4b6d">Cool! This is one way how to fetch data with Redux and Redux Thunk. I like the error handling. Also, it’s possible to use <code>async</code> and <code>await</code> here as well.</p><p id="0d24"><i>Notice the beautiful error handling — this is the biggest reason we are using a Thunk action here.</i></p><p id="fe91">Next up — creating our action types. Start by creating a file in <code>src/Utils</code> called <code>ActionTypes.js</code></p><figure id="c41f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UDX2nBvhPUwJmCE34sGf8Q.png"><figcaption></figcaption></figure><p id="adc2">Import the action types to our action.</p><figure id="d404"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wiG83iR74RXOQC2c_ZPFMw.png"><figcaption></figcaption></figure><p id="4260">Well done! Our action for fetching the data is complete! We need to pass our payload to the reducer and also map the action to our component.</p><h1 id="e981">Implementing The Reducers</h1><p id="b645">Imagine our payload of something like this:</p><figure id="a4e6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fhNnE4Ul0KYycCWfN8FsRA.png"><figcaption></figcaption></figure><p id="b0a4">It’s just raw data. We need to pass this data to our reducer.</p><p id="93ff">Open our <code>CryptoReducer.js</code> and define our <code>initialState</code></p><p id="59d0">We need:</p><ul><li>State for data being fetched so we can show a spinner.</li><li>The state that holds the actual data from the request.</li><li>Error handling — do we have an error and if so what does it say?</li></ul><figure id="a32e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cdO8H6uSmOx2ofjuZd_4Wg.png"><figcaption></figcaption></figure><p id="9ce3">Next up — implementing the <code>switch() </code>conditional and importing the action types.</p><figure id="f7a1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GQU-VXQdKY34vFdNS5U3KQ.png"><figcaption></figcaption></figure><p id="7e58">I’m using the <code>Object.assign()</code> method for creating new objects.

Options

You’re free to use <code>object spread</code> as well.</p><p id="1429">What’s the point of writing code like this? Easy one — maintainability! Yes, it’s quite a lot of boiler code for fetching data — but it’s the right way!</p><p id="a780"><code>case FETCHING_COIN_DATA</code> — is for when we are fetching the data and showing the spinner. React handles all the vDOM updates, we just need to pass our state.</p><p id="a517">Try to implement the cases for <code>FETCHING_COIN_DATA_SUCCESS</code> and <code>FETCHING_COIN_DATA_FAILURE</code></p><p id="02e2">Solution down below:</p><figure id="aac3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*vSNh9dTX0o2x8Z244YhCDQ.png"><figcaption></figcaption></figure> <figure id="0788"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FKC76Eqv.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FKC76Eqv.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FKC76Eqvh.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=video%2Fmp4&amp;schema=imgur" allowfullscreen="" frameborder="0" height="375" width="635"> </div> </div> </figure></iframe></div></div></figure><p id="3f10">Now the only thing to left for us is to call the <code>FetchCoinData</code> action!</p><h1 id="ed16">Calling FetchCoinData action!</h1><p id="86aa">Try to implement it. Here are the steps.</p><ul><li>Import the action to <code>CryptoContainer.js</code></li><li>Map the <code>FetchCoinData </code>inside connect argument.</li><li>call <code>this.props.FetchCoinData() </code>inside<code> componentDidMount()</code></li></ul><figure id="33bc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*uW2v4Cd60bNMJ9fRmBujUQ.png"><figcaption></figcaption></figure><p id="96ff">And that’s it!</p><p id="24a8">Refresh the simulator (mac: <i>cmd + R</i>, Windows: <i>Ctrl + R</i>), open Redux Devtools and you should see the data under <code>crypto</code> state.</p><figure id="2783"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pHVq2i3GArvFHWNl7WX2sg.png"><figcaption>Note: If it’s not showing up — try restarting the simulator</figcaption></figure><p id="a5b7">We have our data mapped under <code>this.props.crypto</code><i>perfect</i>! Let’s implement the views in the next chapter!</p><p id="6e21">Thanks for making it this far, you truly are a warrior and you have what it takes to come a React + Redux pro. <i>Consistency</i>, <i>learning every day,</i> <i>never giving up</i> are the traits you possess! Well done!</p><p id="749e"><a href="https://readmedium.com/learn-how-to-build-a-rn-redux-cryptocurrency-app-chapter-iv-b0e0c5ca2dca">Seeya in the next and final chapter!</a></p><h1 id="e641">Oh and — here’s the Source code!</h1></article></body>

Let’s Build: Cryptocurrency Native Mobile App With React Native + Redux — Chapter III

Welcome back to developing native apps with React Native and Redux like a boss! Glad to have you here! ❤

This is a pre-sequel to chapter II.

Where we left off

Fetching The Data With Redux and Redux Thunk

We’re finally ready to fetch our data. We’re going to use the coinmarketcap public API. If you find a better crypto API — please let me know!

Make a src/Utils/Constants.js file and add our API base URL there.

Why? If the API URL ever changes — we only need to change it in one place, neato! 👍

Next— head over to src/Actions and make a file called FetchCoinData.js — here we will be fetching our cryptocurrency coin data. We’re going to use axios (yes I’m aware of the native fetch API) — I still very much prefer axios.

Quick google search shows why (more options, better API, better error handling).

npm i --save axios

Import axios and the apiBaseURL from Constants.js — also, add the boilerplate for the action. Try to implement this on your own. Remember, an action is just a function which gets exported.

Ready?

Creating Actions

action boilerplate

Next, we return a thunk action. Thunk actions are functions. Why Thunk? More control. I will show you exactly what I mean.

This might seem a bit confusing. We have a function, which returns a function? Yep. This is Thunk doing its magic.

Think about this for a second. When requesting data, there are 3 states.

  1. Requesting the data.
  2. Data Requesting Successful (200 — got the data!)
  3. Data Requesting Fail

This makes perfect sense. Think it of as a promise, there’s calling the Promise, resolving the promise and ejecting the promise.

Let’s finish implementing the rest of the thunk action. Try to implement fetching the data.

Cool! This is one way how to fetch data with Redux and Redux Thunk. I like the error handling. Also, it’s possible to use async and await here as well.

Notice the beautiful error handling — this is the biggest reason we are using a Thunk action here.

Next up — creating our action types. Start by creating a file in src/Utils called ActionTypes.js

Import the action types to our action.

Well done! Our action for fetching the data is complete! We need to pass our payload to the reducer and also map the action to our component.

Implementing The Reducers

Imagine our payload of something like this:

It’s just raw data. We need to pass this data to our reducer.

Open our CryptoReducer.js and define our initialState

We need:

  • State for data being fetched so we can show a spinner.
  • The state that holds the actual data from the request.
  • Error handling — do we have an error and if so what does it say?

Next up — implementing the switch() conditional and importing the action types.

I’m using the Object.assign() method for creating new objects. You’re free to use object spread as well.

What’s the point of writing code like this? Easy one — maintainability! Yes, it’s quite a lot of boiler code for fetching data — but it’s the right way!

case FETCHING_COIN_DATA — is for when we are fetching the data and showing the spinner. React handles all the vDOM updates, we just need to pass our state.

Try to implement the cases for FETCHING_COIN_DATA_SUCCESS and FETCHING_COIN_DATA_FAILURE

Solution down below:

Now the only thing to left for us is to call the FetchCoinData action!

Calling FetchCoinData action!

Try to implement it. Here are the steps.

  • Import the action to CryptoContainer.js
  • Map the FetchCoinData inside connect argument.
  • call this.props.FetchCoinData() inside componentDidMount()

And that’s it!

Refresh the simulator (mac: cmd + R, Windows: Ctrl + R), open Redux Devtools and you should see the data under crypto state.

Note: If it’s not showing up — try restarting the simulator

We have our data mapped under this.props.cryptoperfect! Let’s implement the views in the next chapter!

Thanks for making it this far, you truly are a warrior and you have what it takes to come a React + Redux pro. Consistency, learning every day, never giving up are the traits you possess! Well done!

Seeya in the next and final chapter!

Oh and — here’s the Source code!

React
JavaScript
Mobile App Development
Tech
Programming
Recommended from ReadMedium