avatarTrevor-Indrek Lasn

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

4507

Abstract

://cdn-images-1.readmedium.com/v2/resize:fit:800/1*35HhSJwXTIu_k6DEjGsDVQ.png"><figcaption></figcaption></figure><figure id="ad8b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*W8SWhhnTT8d_5mhm0k7vBA.png"><figcaption></figcaption></figure><p id="4ddb">Note: If you feel this might be too much for you, try building a couple of websites with plain HTML/CSS/JS, and come back when you feel more confident — it can be hard when you’re just starting out.</p><p id="59d8">Picking up some pace now.</p><ol><li>We make a new <code>directory </code>inside <code>src/app</code><b> </b>and we call it <code>components</code>.</li><li>Inside <code>components</code>, we make two additional files, called <code>AstronomyContainer.js</code> and <code>AstronomyCard.js</code>.</li><li>Let’s clean up <code>src/app/App.js</code> and import<code>AstronomyContainer.js</code>.</li></ol><figure id="e75a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*11eDGykDD92JPd1xTSVkKQ.png"><figcaption>src/app/App.js</figcaption></figure><h1 id="cf86">Stateful and Stateless</h1><p id="fc57">There are a couple of things we need to do now.</p><p id="4ad2">We need to write two components, one of them is called <code>stateful</code> (aka the smart/dynamic component) and the second one is <code>stateless</code> (aka the dumb/dummy/static component).</p><p id="346b">Can you guess which is which?</p><ul><li>Stateless — Always returns the same output. Component where data “flows” inside. Mostly used for representing something. Does not include the state, only props!</li><li>Stateful — Has some activity and the component has an internal state. Examples of activity: data fetching, user interaction (for example button clicks), and data passing.</li></ul><p id="595b">Hopefully that didn’t sound too complicated, it’s a really simple concept.</p><p id="c993">Let’s write the backbone for the two components.</p><figure id="707a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*3h7m1xOGNWdEPAsMQuiVbw.png"><figcaption></figcaption></figure><figure id="bfc5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QdkOBIwPpCukIvpQm0w34g.png"><figcaption></figcaption></figure><p id="fcd6">Notice how one of the components is <code>class</code> based and the second is not?</p><p id="56fd">The class based component will carry the state. Why do we need state? We need state, so we can save what’s happening with our dynamic application.</p><p id="608c">Think about it. If a user clicks a button, how does the app know what to do next? Before clicking, we have a default state. After the user clicks the button, we change the state to represent what will happen next.</p><p id="b5a1">Example<b>:</b></p><p id="8210">We have a counter with a starting number at zero. The initial state is zero. The user clicks on the button. How are we going to fit the pieces together, and more importantly, increment or decrement the number?</p><p id="824e">We need some kind of dynamic mechanism to represent our actions, and convert it into representational feedback/data. We call it <i>State</i>. Once you use state — you can’t live without it.</p><p id="def2">Back to building the app. Take a look at the <a href="https://api.nasa.gov/planetary/apod?api_key=nxKl8yTvpvsXEqRz06mTPnn29uyckFmFCYrnqEIz">NASA astronomy picture of the day endpoint</a>.</p><p id="49cc">Make sure you look at the URL, and <b>change the <code>api_key=</code></b> to your personal key.</p><figure id="6803"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*BcZg_izSSgm5nfQRYJntpg.png"><figcaption>NASA data</figcaption></figure><p id="bdcc">We need to somehow fetch the data. Lucky for us this is relatively easy.</p><p id="2211">Go to the root of your project and install <a href="https://github.com/axios/axios">Axios</a>.</p><p id="57f9"><code>npm i axios</code></p><p id="a35a">I’ll show you some code now. Try to understand it — I will explain it below.</p><p id="fb10">Don’t forget to replace the API key with your personal one.</p><figure id="fc2c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*lnBesUFzFbaFbt2ZXm7EgA.png"><figcaption></figcaption></figure><figure id="05b9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*2g5aCiUdRtgra9pcMAwD0Q.png"><figcaption></figcaption></figure><ol><li>We import <code>axios</code></li><li>We call the <code>constructor()</code><b> </b>method where we initialize our state. The initial state is just an em

Options

pty array.</li><li>We call a lifecycle called <code>componentDidMount</code> and inside the lifecycle method we declare our <code>END_POINT</code> and <code>API_KEY</code> constants.</li><li>Inside the <code>componentDidMount</code> method we call a <code>GET</code> request with <code>axios</code>.</li><li><code>Axios</code> returns a promise, and inside that promise we have access to the data. The data is under <code>response.data</code>.</li></ol><p id="4740">If you open the browser console you should be able to see the response.</p><p id="98c0">We’re almost there. Now we need to save the response to the current state.</p><figure id="cf92"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bFpcQxli3Vdhvf16UXbhKg.png"><figcaption></figcaption></figure><p id="fb01">We call the <code>setState()</code> method and pass in our state object.</p><p id="8dff">If we <code>console.log()</code> our state, we should see the data! One last step with the current component, we also need to pass this state down to our child/stateless component.</p><figure id="e03e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bfS00wSlm_O_9gDlVeLitg.png"><figcaption></figcaption></figure><p id="fa9f">We pass the <code>AstronomyCard</code> a prop as our state. I’ve included <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">fancy little object destructuring.</a></p><p id="5e3e">So how do we use the parent data inside our sibling? Turns out, it’s not hard — React is easy and intuitive to work with.</p><p id="4041">We have access to the data under <code>props.data</code> in our sibling. What I’m doing is deconstructing the object, so we don’t have to write out the long props each time. Instead of <code>props.data.title</code> we can write <code>{title}</code>.</p><figure id="f980"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*O9R5VsdfTqVz0wQ_hyOYRw.png"><figcaption></figcaption></figure><p id="5daa"><a href="https://gist.github.com/wesharehoodies/9b6d56699b2b3c38575d0c09f8314738">One last thing. Grab the styles here and place them i</a>n <code>src/assets/stylesheets/styles.scss</code></p><p id="7f06">Let’s take look at our app.</p><figure id="422e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*aXEgLQqr8U-o9xXuK-FFaA.gif"><figcaption></figcaption></figure><p id="39e4">Not too overwhelming, but we definitely got a lot done today. There is so much more you can do with this API — here is the <a href="https://api.nasa.gov/api.html#apod">full list of endpoints</a> — feel free to experiment.</p><p id="d4a5">Our next goal will be refactoring using Redux, which will be covered in the next chapter. Redux is complicated and I will dedicate a full chapter just for Redux.</p><p id="a0a7"><a href="https://github.com/wesharehoodies/nasa-react-redux/blob/master/src/app/components/AstronomyCard.js">NASA may include videos instead of URLs. Check out the <code>AstronomyCard</code>.js file here for the solution</a>.</p><h1 id="8537">Part II</h1><div id="fe0c" class="link-block"> <a href="https://readmedium.com/learn-how-to-build-astronomy-picture-of-the-day-app-with-nasa-api-and-react-redux-part-ii-83f15970d0e3"> <div> <div> <h2>Learn how to build an Astronomy Picture of the Day App with the NASA API and React + Redux (Part…</h2> <div><h3>Aha! So you’re back! Good! We will be dedicating this whole chapter to Redux.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*h8UwBRQySOtTklrMnTy8Dw.png)"></div> </div> </div> </a> </div><h1 id="c462">Source Code</h1><div id="9a3d" class="link-block"> <a href="https://github.com/indreklasn/nasa-react-redux/tree/chapter-1"> <div> <div> <h2>indreklasn/nasa-react-redux</h2> <div><h3>Guide: Build a React + Redux app. Contribute to indreklasn/nasa-react-redux development by creating an account on…</h3></div> <div><p>github.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*vuCqcWXJVDvCoucY)"></div> </div> </div> </a> </div></article></body>

Learn How To Build: Astronomy Picture of the Day App With NASA API and React + Redux — Part I

Demo

I recently found out that NASA has a public API. Cool! Let’s build something with the API today!

What are we going to build? We’re going to build an app called astronomy picture of the day!

Prerequisites

Basic JavasScript experience.

We are using React for building the application UI, Axios for fetching data and Redux for managing our application state.

Also, let’s not forget about Webpack, which will compile it all down for our browser. Great workflow!

Step 1 — Scaffolding

Please grab our Webpack config from this repository.

  1. We’re going to rename the current project to nasa-react-redux.
  2. Change directory, jump to branch chapter-5 and do a git pull origin chapter-5.
  3. Remove .git since we don’t want our little project to be associated with the Webpack guide repository.
  4. Install npm dependencies: npm install or npm i for short.

Let’s boot up our Webpack config and open the project with our favourite text editor.

The command to boot up Webpack is npm run start and you should see something like the following if everything works correctly.

You’ve made it past the scaffolding part, on to step two!

Step 2 — Sign Up for a NASA API Key

Head over to the official NASA website and sign up for a API key. It’s very straight forward. The key should be available instantly.

Signing up and receiving the API key

The API key will be sent to your email as well. Store the key somewhere safe.

Step 3 — Building the App Itself With React

As this is a fairly easy application to build, let’s begin by building the app with just React itself, and after that with React+Redux so we can compare the workflow. We’ll also understand why we even need a state container like Redux.

Brief explanation on how React is setup currently:

We have an index.html inside public/ and a index.js inside src/.

In a nutshell — we have a #root div inside index.html which hooks up to React. We let Webpack do all the magic of compiling our React to plain JavaScript which the browser reads.

Obviously this is a ridiculously oversimplified explanation, but you get the main concept.

Note: If you feel this might be too much for you, try building a couple of websites with plain HTML/CSS/JS, and come back when you feel more confident — it can be hard when you’re just starting out.

Picking up some pace now.

  1. We make a new directory inside src/app and we call it components.
  2. Inside components, we make two additional files, called AstronomyContainer.js and AstronomyCard.js.
  3. Let’s clean up src/app/App.js and importAstronomyContainer.js.
src/app/App.js

Stateful and Stateless

There are a couple of things we need to do now.

We need to write two components, one of them is called stateful (aka the smart/dynamic component) and the second one is stateless (aka the dumb/dummy/static component).

Can you guess which is which?

  • Stateless — Always returns the same output. Component where data “flows” inside. Mostly used for representing something. Does not include the state, only props!
  • Stateful — Has some activity and the component has an internal state. Examples of activity: data fetching, user interaction (for example button clicks), and data passing.

Hopefully that didn’t sound too complicated, it’s a really simple concept.

Let’s write the backbone for the two components.

Notice how one of the components is class based and the second is not?

The class based component will carry the state. Why do we need state? We need state, so we can save what’s happening with our dynamic application.

Think about it. If a user clicks a button, how does the app know what to do next? Before clicking, we have a default state. After the user clicks the button, we change the state to represent what will happen next.

Example:

We have a counter with a starting number at zero. The initial state is zero. The user clicks on the button. How are we going to fit the pieces together, and more importantly, increment or decrement the number?

We need some kind of dynamic mechanism to represent our actions, and convert it into representational feedback/data. We call it State. Once you use state — you can’t live without it.

Back to building the app. Take a look at the NASA astronomy picture of the day endpoint.

Make sure you look at the URL, and change the api_key= to your personal key.

NASA data

We need to somehow fetch the data. Lucky for us this is relatively easy.

Go to the root of your project and install Axios.

npm i axios

I’ll show you some code now. Try to understand it — I will explain it below.

Don’t forget to replace the API key with your personal one.

  1. We import axios
  2. We call the constructor() method where we initialize our state. The initial state is just an empty array.
  3. We call a lifecycle called componentDidMount and inside the lifecycle method we declare our END_POINT and API_KEY constants.
  4. Inside the componentDidMount method we call a GET request with axios.
  5. Axios returns a promise, and inside that promise we have access to the data. The data is under response.data.

If you open the browser console you should be able to see the response.

We’re almost there. Now we need to save the response to the current state.

We call the setState() method and pass in our state object.

If we console.log() our state, we should see the data! One last step with the current component, we also need to pass this state down to our child/stateless component.

We pass the AstronomyCard a prop as our state. I’ve included fancy little object destructuring.

So how do we use the parent data inside our sibling? Turns out, it’s not hard — React is easy and intuitive to work with.

We have access to the data under props.data in our sibling. What I’m doing is deconstructing the object, so we don’t have to write out the long props each time. Instead of props.data.title we can write {title}.

One last thing. Grab the styles here and place them in src/assets/stylesheets/styles.scss

Let’s take look at our app.

Not too overwhelming, but we definitely got a lot done today. There is so much more you can do with this API — here is the full list of endpoints — feel free to experiment.

Our next goal will be refactoring using Redux, which will be covered in the next chapter. Redux is complicated and I will dedicate a full chapter just for Redux.

NASA may include videos instead of URLs. Check out the AstronomyCard.js file here for the solution.

Part II

Source Code

React
Redux
NASA
JavaScript
Programming
Recommended from ReadMedium