avatarTrevor-Indrek Lasn

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

4586

Abstract

.</p><h1 id="419e">Creating the Store</h1><p id="acd1">Create <code>src/Store.js</code> and add the following contents.</p><p id="3708"><i>Note:</i> I tried to simplify this as much as possible! Don’t get nervous, I will explain all the parts. There’s a lot of magic going on but we can grab the bull by the horns.</p><figure id="77e5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VZdFygBZ2rBS3RGuJHT_mA.png"><figcaption></figcaption></figure><p id="bef9">Starting from the top.</p><ul><li>import <a href="https://facebook.github.io/react-native/docs/platform-specific-code.html#platform-module"><code>Platf</code>orm</a> from <code>‘react-native’</code></li></ul><p id="efef">React Native provides a module that detects the platform in which the app is running. You can use the detection logic to implement platform-specific code. Use this option when only small parts of a component are platform-specific.</p><p id="1021">We need <code>Platform</code> for our remote dev tools.</p><ul><li><code>createStore()</code> is needed to create our initial store. We pass this store to the <code><Provider></code></li></ul><p id="b284"><code>createStore()</code> <a href="https://github.com/reactjs/redux/blob/master/docs/api/createStore.md">accepts the following arguments</a>:</p><ul><li><b>reducer</b></li><li><b>preloaded state</b></li><li><b>enhancer</b></li><li><code>applyMiddleware()</code> — Is for applying.. you guessed it… the middleware. Remember we are using <code>thunk</code> actions, <code>promise</code> and <code>logger</code>.</li><li><code>compose()</code> — Composes functions from right to left.</li></ul><p id="e148">This is a functional programming utility and is included in Redux as a convenience. You might want to use it to apply several <a href="https://github.com/reactjs/redux/blob/master/docs/Glossary.md#store-enhancer">store enhancers</a> in a row. —<a href="https://github.com/reactjs/redux/blob/master/docs/api/compose.md">More information.</a></p><p id="3f73">We pass our middleware and devTools as the arguments for <code>compose</code> function.</p><ul><li>import our <code>RootReducer</code> and pass it as the first argument to the <code>createStore()</code> function.</li><li>Lastly, we expose our store to the outside world.</li></ul><p id="5a40">It’s completely natural to be overwhelmed — in fact, if you’re not overwhelmed, congratulations for being a <i>prodigy</i>. Take some time to read this couple times.</p><p id="b34e"><b><i>Help: <a href="https://github.com/reactjs/redux/blob/master/docs/api"></a></i></b><a href="https://github.com/reactjs/redux/blob/master/docs/api">Head over here if it’s just not clicking.</a></p><figure id="b310"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KL_HB7IYLAXHz8zyG5wFvg.gif"><figcaption></figcaption></figure><h1 id="01cd">Adding the Provider</h1><p id="1996">Head over back to <code>App.js</code> and import our <code>Store.js</code> and <code>Provider</code> from <code>‘react-redux’</code></p><figure id="512b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*SXRdkLa3oUdW-R7InTID6g.png"><figcaption></figcaption></figure><p id="61f0">The <code>Provider</code> accepts one argument — which is our store. The <code>Provider</code> role is to glue <b><i>React</i></b> and <b><i>Redux</i></b> together.</p><h1 id="d5f6">Final steps with tooling</h1><p id="9d9e">Our final tooling step is to add a couple script to the <code>package.json</code></p><ul><li>Add this line to the <code><b><i>scripts</i></b></code></li></ul><p id="2d9c"><code>“postinstall”: “remotedev-debugger — hostname localhost — port 5678 — injectserver”</code></p><ul><li>Add a new object called <code>“remotedev”</code> with these two props:</li></ul><div id="d7f6"><pre><span class="hljs-attr">"remotedev"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span></pre></div><div id="2997"><pre> <span class="hljs-attr">"hostname"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"localhost"</span><span class="hljs-punctuation">,</span></pre></div><div id="4c7f"><pre> <span class="hljs-attr">"port":</span> <span class="hljs-number">5678</span></pre></div><div id="26e5"><pre>},</pre></div><figure id="6391"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*o_7Nu6deP15RSEUBDzRvyQ.png"><figcaption></figcaption></figure><h1 id="986a">Creating our RootReducer</h1><p id="07f3"><code>$ mkdir src/Reducers && cd src/Reducers && touch index.js && touch CryptoReducer.js</code><

Options

/p><figure id="a354"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rKO46xPa2CJcRC6hqD_kwQ.png"><figcaption></figcaption></figure><figure id="1da0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GUYgz9h8uINPfUTkKb9K9Q.png"><figcaption></figcaption></figure><p id="5e82">Remember <b><i>reducers</i></b> are async by default — that’s they are anonymous functions — all <b><i>reducers</i></b> get invoked after an action. That’s why we have the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch"><b>switch conditional</b></a><b> </b>to handle appropriate actions.</p><h2 id="e7d7">Create our crypto container and map it to the redux state</h2><p id="e305"><code>touch src/components/CryptoContainer.js</code></p><figure id="2ce2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Yhj536NHU_8QTgf9jgwPXw.png"><figcaption></figcaption></figure><p id="8162">We use the connect method to hook React components with Redux state. <code><b>connect()</b></code><b> </b>accepts two arguments.</p><ul><li>The first argument is mapStateToProps. Does exactly what it’s called after. Maps the Redux state to our React props. We can access the <code>crypto</code> state under <code>this.props.crypto</code></li><li>Second argument is mapDispatchToAction. We will get there in the next chapter.</li></ul><p id="43e5">Make the <code>Header.js</code> and <code>CryptoContainer</code> exportable in <code>src/components/index.js</code> — like so</p><figure id="4633"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*K1Gq3hczEEEYoe6qOPcRVA.png"><figcaption><b>src/components/index.js</b></figcaption></figure><p id="59af">Finally, let’s add the <code>CryptoContainer.js</code> component to <code>App.js</code> like so:</p><figure id="4e9c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_tJzUda31oF3u9Q8wWLugA.png"><figcaption></figcaption></figure><h1 id="f24a">Time to test!</h1><p id="975b">Run our <i>iOS</i>/<i>Android</i> simulator.</p><figure id="9dbe"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ToCn7T8GNc6OQlqoNw1MOg.png"><figcaption></figcaption></figure><p id="2bd6">This is what you should see:</p><figure id="53de"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*BxherIB8h5seWFCfqpMXGQ.png"><figcaption></figcaption></figure><p id="d872">Alright. So how do we debug? Great question! Press these keys:</p><p id="2f14"><i>Mac</i>: <b><i>cmd</i></b> +<b><i> d</i></b></p><p id="eca7"><i>Windows</i>: <b>Ctrl</b> +<b> M</b></p><h2 id="7217">This opens the options for our react native app.</h2><figure id="adfe"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5abupY21ztBn0e9PVeYPlw.png"><figcaption></figcaption></figure><p id="328b">Click on <b><i>Debug Remote JS.</i></b> This pops up a browser in debug mode.</p><figure id="cf83"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*m_y1-FooQYVvcXOt4tSeKg.png"><figcaption></figcaption></figure><p id="c321">Works flawlessly. As we can see with our <i>Redux Devtools</i> —the <code><b><i>crypto</i></b></code> state being show (empty array).</p><p id="52bf">Wow, that was a lot of boiler code and setup. I guarantee it’s worth it. <i>Redux</i> is cool.</p><p id="ab65">This chapter went all on tooling and setup. Next chapter we will implement the actual data fetching and write some “non-pseudo” code.</p><p id="3260">Thanks for making it this far, you’re <b><i>awesome</i></b>! 😊</p><h1 id="0ddf">Continue here!</h1><div id="9ceb" class="link-block"> <a href="https://readmedium.com/learn-how-to-build-a-rn-redux-cryptocurrency-app-chapter-iii-a454dda156b"> <div> <div> <h2>Let’s Build: Cryptocurrency Native Mobile App With React Native + Redux — Chapter III</h2> <div><h3>Welcome back to developing native apps with React Native and Redux like a boss! Glad to have you here! ❤</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*fhNnE4Ul0KYycCWfN8FsRA.png)"></div> </div> </div> </a> </div><p id="92e8">If you want to bring your JavaScript to the next level, start from <a href="https://amzn.to/2k7AZHj"><i>this book</i></a><i>, f</i>ind the kindle version <a href="https://amzn.to/2XJ0KQY"><i>here</i></a>.</p><h1 id="7da8">Source code in case you got lost!</h1></article></body>

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

Welcome back! Here’s the chapter I.

We left off implementing the header but missing the styles. How do we implement them? As you remember from our previous chapter — we can import the StyleSheet prop from ‘react-native’. This gives us the possibility of styling our elements. A StyleSheet is an object — which contains a create() method. We pass an object which are our styles to the create() method.

Here’s the implementation. First, we import the StyleSheet. Secondly, we define our styles with StyleSheet.create() and pass our style object as n argument. Thirdly we deconstruct our props from the styles variable. And last we place style props to our elements via the style property. Hope this makes sense. Make sure you understand the concepts mentioned — make sure to read and understand the links I provided (underlined text). 😊

Fetching data from the API with Redux.

this one should be familiar. Setting Redux with RN is the same process as setting it up with React. Try to do it solo first, you will learn more this way! Here’s a guide on mostly Redux. You can do it!

Ready?

I start by installing dependencies. Here’s the list of dependencies we’re going to need.

$ npm i --save redux react-redux redux-thunk redux-promise && npm i --save-dev remote-redux-devtools remotedev-rn-debugger remote-redux-devtools redux-logger

Tooling explained:

  • redux — Redux library.
  • react-redux — Glue for React and Redux.
  • remote-redux-devtool remotedev-rn-debugger

These are Redux debugging middleware, allows us to use the Redux Chrome Devtools in Remote mode.

NB: Make sure you download this chrome extension!!

Redux Devtools extension for Chrome
  • redux-thunk Redux thunk lets us return a function inside an action instead of returning an object.
Demonstration
  • redux-logger — logs the previous state, action and the next state in the console.

Note: logger must be the last middleware in the chain, otherwise it will log thunk and promise, not actual actions (#20).

  • redux-promise — Allows us to resolve async actions (promises) with Redux. Example: Fetching data from an API.

I hope you’re fairly caffeinated — we’re gonna start playing around with our store.

Creating the Store

Create src/Store.js and add the following contents.

Note: I tried to simplify this as much as possible! Don’t get nervous, I will explain all the parts. There’s a lot of magic going on but we can grab the bull by the horns.

Starting from the top.

React Native provides a module that detects the platform in which the app is running. You can use the detection logic to implement platform-specific code. Use this option when only small parts of a component are platform-specific.

We need Platform for our remote dev tools.

  • createStore() is needed to create our initial store. We pass this store to the <Provider>

createStore() accepts the following arguments:

  • reducer
  • preloaded state
  • enhancer
  • applyMiddleware() — Is for applying.. you guessed it… the middleware. Remember we are using thunk actions, promise and logger.
  • compose() — Composes functions from right to left.

This is a functional programming utility and is included in Redux as a convenience. You might want to use it to apply several store enhancers in a row. —More information.

We pass our middleware and devTools as the arguments for compose function.

  • import our RootReducer and pass it as the first argument to the createStore() function.
  • Lastly, we expose our store to the outside world.

It’s completely natural to be overwhelmed — in fact, if you’re not overwhelmed, congratulations for being a prodigy. Take some time to read this couple times.

Help: Head over here if it’s just not clicking.

Adding the Provider

Head over back to App.js and import our Store.js and Provider from ‘react-redux’

The Provider accepts one argument — which is our store. The Provider role is to glue React and Redux together.

Final steps with tooling

Our final tooling step is to add a couple script to the package.json

  • Add this line to the scripts

“postinstall”: “remotedev-debugger — hostname localhost — port 5678 — injectserver”

  • Add a new object called “remotedev” with these two props:
"remotedev": {
  "hostname": "localhost",
  "port": 5678
},

Creating our RootReducer

$ mkdir src/Reducers && cd src/Reducers && touch index.js && touch CryptoReducer.js

Remember reducers are async by default — that’s they are anonymous functions — all reducers get invoked after an action. That’s why we have the switch conditional to handle appropriate actions.

Create our crypto container and map it to the redux state

touch src/components/CryptoContainer.js

We use the connect method to hook React components with Redux state. connect() accepts two arguments.

  • The first argument is mapStateToProps. Does exactly what it’s called after. Maps the Redux state to our React props. We can access the crypto state under this.props.crypto
  • Second argument is mapDispatchToAction. We will get there in the next chapter.

Make the Header.js and CryptoContainer exportable in src/components/index.js — like so

src/components/index.js

Finally, let’s add the CryptoContainer.js component to App.js like so:

Time to test!

Run our iOS/Android simulator.

This is what you should see:

Alright. So how do we debug? Great question! Press these keys:

Mac: cmd + d

Windows: Ctrl + M

This opens the options for our react native app.

Click on Debug Remote JS. This pops up a browser in debug mode.

Works flawlessly. As we can see with our Redux Devtools —the crypto state being show (empty array).

Wow, that was a lot of boiler code and setup. I guarantee it’s worth it. Redux is cool.

This chapter went all on tooling and setup. Next chapter we will implement the actual data fetching and write some “non-pseudo” code.

Thanks for making it this far, you’re awesome! 😊

Continue here!

If you want to bring your JavaScript to the next level, start from this book, find the kindle version here.

Source code in case you got lost!

Redux
React
Mobile App Development
Cryptocurrency
JavaScript
Recommended from ReadMedium