<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: A Faruk Gonullu</title>
    <description>The latest articles on DEV Community by A Faruk Gonullu (@afgonullu).</description>
    <link>https://dev.to/afgonullu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F473516%2Fd93fb255-b1c1-48f8-a0ea-64d730493f51.jpg</url>
      <title>DEV Community: A Faruk Gonullu</title>
      <link>https://dev.to/afgonullu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/afgonullu"/>
    <language>en</language>
    <item>
      <title>Build Country Browser using React and 2 APIs</title>
      <dc:creator>A Faruk Gonullu</dc:creator>
      <pubDate>Fri, 27 Nov 2020 12:35:44 +0000</pubDate>
      <link>https://dev.to/afgonullu/build-country-browser-using-react-and-2-apis-mia</link>
      <guid>https://dev.to/afgonullu/build-country-browser-using-react-and-2-apis-mia</guid>
      <description>&lt;p&gt;Live Demo is here: &lt;a href="https://country-browser-azure.vercel.app/" rel="noopener noreferrer"&gt;https://country-browser-azure.vercel.app/&lt;/a&gt;&lt;br&gt;
Repo is here: &lt;a href="https://github.com/afgonullu/country-browser" rel="noopener noreferrer"&gt;https://github.com/afgonullu/country-browser&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will build a Country Browser App using React, React Bootstrap and 2 APIs, REST Countries API and Weatherstack API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-5.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2Ffinal-ss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-5.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2Ffinal-ss.png" alt="final image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting Up the Project
&lt;/h2&gt;

&lt;p&gt;Create a new React Project using the boilerplate provided.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app country-browser&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After everything is finished, if we run &lt;code&gt;npm start&lt;/code&gt;, we will see that our React app is running and a spinning React logo centered in the page.&lt;/p&gt;

&lt;p&gt;There are a couple of files, that we won't be using. You can leave them as they are or delete them. If you want to have a clear and uncluttered structure, delete these files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;country-browser
└── src
    ├── App.css
    ├── App.test.js
    ├── logo.svg
    ├── reportWebVitals.js
    └── setupTests.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we removed these files, our app will stop working properly. We need to adjust and clean up couple of stuff in &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;app.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"
import ReactDOM from "react-dom"
import "./index.css"
import App from "./App"

ReactDOM.render(&amp;lt;App /&amp;gt;, document.getElementById("root"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = (props) =&amp;gt; {
  return &amp;lt;h1&amp;gt;Hello World. Welcome to Country Browser&amp;lt;/h1&amp;gt;
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also clean up the project dependencies in &lt;code&gt;package.json&lt;/code&gt;. Should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;///
...
"dependencies": {
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1"
  },
...
///
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we run again using &lt;code&gt;npm start&lt;/code&gt;, we will see our magical words on the browser. This is the starting point for our project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-2.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-2.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2Fimage.png" alt="first version"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Designing
&lt;/h2&gt;

&lt;p&gt;On a full scale project you would want to have a complete design system. For example, Notion offers a template like this one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-4.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2FDesign-System--Design-System.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-4.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2FDesign-System--Design-System.png" alt="design system"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this project, we will just decide on the layout and colors. &lt;/p&gt;

&lt;p&gt;For the layout, let's say that;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will use a left sidebar and list all the countries.&lt;/li&gt;
&lt;li&gt;Sidebar will also have a search functionality, that will help user filter the countries&lt;/li&gt;
&lt;li&gt;When a country is selected, Main Section will be populated according to the selected country.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As for the colors; let's go to coolors.co and pick the first random color scheme:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-5.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2Fscheme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-5.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2Fscheme.png" alt="color scheme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's use white as the background color and rest of the colors will be theme colors. We can check out the contrast of the colors using &lt;a href="https://webaim.org/resources/contrastchecker/" rel="noopener noreferrer"&gt;Webaim Contrast Checker Tool&lt;/a&gt;. That way we will make sure that our colors looks accessible and readable against background.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layout and Theme Implementation
&lt;/h2&gt;

&lt;p&gt;Bootstrap is a great tool and React Bootstrap library is a great tool on top of a great tool to create our UI foundation. Let's install first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-bootstrap bootstrap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will also install bootstrap, because we want to make simple customizations to the Bootstrap theme. Also we &lt;a href="https://create-react-app.dev/docs/adding-bootstrap/#:~:text=To%20enable%20scss%20in%20Create,need%20to%20install%20node%2Dsass%20.&amp;amp;text=To%20customize%20Bootstrap%2C%20create%20a,the%20imported%20file(s)." rel="noopener noreferrer"&gt;need to install node-sass&lt;/a&gt;, in order to compile Sass files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save node-sass@4.14.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Node Sass has been updated to v5 and create-react-app package doesn't yet support v5. Therefore, it is important to declare the version when installing.)&lt;/p&gt;

&lt;p&gt;After these, to test that everything is working properly let's make small modifications to our &lt;code&gt;App.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.scss" // custom theme for bootstrap
import { Container, Row, Col } from "react-bootstrap" // React bootstrap components

const App = (props) =&amp;gt; {
  return (
    &amp;lt;Container fluid&amp;gt;
      &amp;lt;Row&amp;gt;
        &amp;lt;Col md="3"&amp;gt;Sidebar&amp;lt;/Col&amp;gt;
        &amp;lt;Col md="9"&amp;gt;Main Section&amp;lt;/Col&amp;gt;
      &amp;lt;/Row&amp;gt;
    &amp;lt;/Container&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;App.scss&lt;/code&gt; file looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import "~bootstrap/scss/bootstrap";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is only one line, where we import the bootstrap .scss file. What ever we wrote above it will customize the vanilla bootstrap. This way, we will have a proper customization and original files will stay clean.&lt;/p&gt;

&lt;p&gt;Let's define our theme colors properly. In order to do that, We will override only bootstrap theme color definitions. It can be found in &lt;code&gt;/node_modules/bootstrap/scss/_variables.scss&lt;/code&gt;. With everything in place, final version of &lt;code&gt;App.scss&lt;/code&gt; looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$theme-white: #ffffff;
$cerulean-crayola: #00a7e1;
$rich-black: #00171f;
$prussian-blue: #003459;
$cg-blue: #007ea7;

$theme-colors: (
  "primary": $prussian-blue,
  "secondary": $cg-blue,
  "info": $cerulean-crayola,
  "light": $theme-white,
  "dark": $rich-black,
);

@import "~bootstrap/scss/bootstrap";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  First API Call
&lt;/h2&gt;

&lt;p&gt;Let's install &lt;code&gt;axios&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Right now, I will not go over REST APIs, HTTP methods, axios or React Hooks. I would write specific blog posts about them in the future and link them here.If you feel that you need to understand them better, please do it now before proceeding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We will use &lt;code&gt;https://restcountries.eu/rest/v2/all&lt;/code&gt; endpoint. If we copy and paste the link to our browser, we will see the response and all kinds of information about the returning object array. This will be important when we are going to filter or manipulate the data.&lt;/p&gt;

&lt;p&gt;Let's make a call to the API to see if we can fetch data, and log the response to the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      const response = await axios.get("https://restcountries.eu/rest/v2/all")

      console.log(response.data)
      setCountries(response.data)
    }

    fetchData()
  }, [])
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we open the console on our browser, we should see an array of 250 objects. &lt;/p&gt;

&lt;p&gt;Ok, time to get serious. First, we need to create a state variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [countries, setCountries] = useState([])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are unfamiliar with useState hook, again I advise you to learn about it. To summarize, useState allows to manage state in functional components in a much more flexible manner.&lt;/p&gt;

&lt;p&gt;We will use &lt;code&gt;countries&lt;/code&gt; variable to store the array returned from our API call. We will make the call when our app renders. Since countries will not change ever, in order to avoid making the call every time the component renders, we slightly modify useEffect hook. &lt;/p&gt;

&lt;p&gt;Final step is to display the data on our page. &lt;code&gt;map&lt;/code&gt; function, as well as other array functions, is a key tool when working with dynamic data. We can simply list the names of the countries in the sidebar by mapping through the &lt;code&gt;countries&lt;/code&gt; variable. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;App.js&lt;/code&gt; looks like below at this point:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from "react"
import axios from "axios"
import "./App.scss"
import { Container, Row, Col, ListGroup, ListGroupItem } from "react-bootstrap"

const App = (props) =&amp;gt; {
  const [countries, setCountries] = useState([])
  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      const response = await axios.get("https://restcountries.eu/rest/v2/all")

      setCountries(response.data)
    }

    fetchData()
  }, [countries])

  return (
    &amp;lt;Container fluid&amp;gt;
      &amp;lt;Row&amp;gt;
        &amp;lt;Col md="3"&amp;gt;
          &amp;lt;ListGroup&amp;gt;
            {countries.map((country) =&amp;gt; (
              &amp;lt;ListGroupItem key={country.name}&amp;gt;{country.name}&amp;lt;/ListGroupItem&amp;gt;
            ))}
          &amp;lt;/ListGroup&amp;gt;
        &amp;lt;/Col&amp;gt;
        &amp;lt;Col md="9"&amp;gt;Main Section&amp;lt;/Col&amp;gt;
      &amp;lt;/Row&amp;gt;
    &amp;lt;/Container&amp;gt;
  )
}

export default App

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Search and Filter
&lt;/h2&gt;

&lt;p&gt;Next step is adding a search and filter functionality. It requires couple of additions and changes to our code structure.&lt;/p&gt;

&lt;p&gt;First of all, we are mapping over &lt;code&gt;countries&lt;/code&gt; at the moment. In order to have a functional sidebar, we need to have a dynamic state, which will represent the result of the search value. Secondly, we need some UI elements and search logic implemented. Therefore we need,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI element, i.e. search form&lt;/li&gt;
&lt;li&gt;Search and filtering logic&lt;/li&gt;
&lt;li&gt;A state variable to store search criteria&lt;/li&gt;
&lt;li&gt;A state variable to store filtered countries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is as simple as a form control element from React Bootstrap library. We used &lt;code&gt;onChange&lt;/code&gt;, because we will implement a logic that will filter at every keystroke.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
&amp;lt;Form&amp;gt;
  &amp;lt;Form.Control
    value={search}
    type="text"
    placeholder="Filter Countries..."
    onChange={handleSearch}
  /&amp;gt;
&amp;lt;/Form&amp;gt;
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;State variables are as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [filtered, setFiltered] = useState([])
  const [search, setSearch] = useState("")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Logic is pretty straightforward. &lt;code&gt;handleSearch&lt;/code&gt; sets the state variable &lt;code&gt;search&lt;/code&gt; after every key stroke. Since &lt;code&gt;search&lt;/code&gt; is changed the component is rerendered and our &lt;code&gt;useEffect&lt;/code&gt; executes again. When it executes, it filters the countries according to the string that is held at &lt;code&gt;search&lt;/code&gt; variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  useEffect(() =&amp;gt; {
    setFiltered(
      countries.filter((country) =&amp;gt;
        country.name.toUpperCase().includes(search.toUpperCase())
      )
    )
  }, [countries, search])

  const handleSearch = (event) =&amp;gt; {
    setSearch(event.target.value)
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we run the app, we will see that search functionality works as intended. Our &lt;code&gt;App.js&lt;/code&gt; looks like this at this stage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from "react"
import axios from "axios"
import "./App.scss"
import {
  Container,
  Row,
  Col,
  ListGroup,
  ListGroupItem,
  Form,
} from "react-bootstrap"

const App = (props) =&amp;gt; {
  const [countries, setCountries] = useState([])
  const [filtered, setFiltered] = useState([])
  const [search, setSearch] = useState("")

  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      const response = await axios.get("https://restcountries.eu/rest/v2/all")

      setCountries(response.data)
    }

    fetchData()
  }, [countries])

  useEffect(() =&amp;gt; {
    setFiltered(
      countries.filter((country) =&amp;gt;
        country.name.toUpperCase().includes(search.toUpperCase())
      )
    )
  }, [countries, search])

  const handleSearch = (event) =&amp;gt; {
    setSearch(event.target.value)
  }

  return (
    &amp;lt;Container fluid&amp;gt;
      &amp;lt;Row&amp;gt;
        &amp;lt;Col md="3"&amp;gt;
          &amp;lt;Form&amp;gt;
            &amp;lt;Form.Control
              value={search}
              type="text"
              placeholder="Filter Countries..."
              onChange={handleSearch}
            /&amp;gt;
          &amp;lt;/Form&amp;gt;
          &amp;lt;ListGroup&amp;gt;
            {filtered.map((country) =&amp;gt; (
              &amp;lt;ListGroupItem key={country.name}&amp;gt;{country.name}&amp;lt;/ListGroupItem&amp;gt;
            ))}
          &amp;lt;/ListGroup&amp;gt;
        &amp;lt;/Col&amp;gt;
        &amp;lt;Col md="9"&amp;gt;Main Section&amp;lt;/Col&amp;gt;
      &amp;lt;/Row&amp;gt;
    &amp;lt;/Container&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Showing Country Details
&lt;/h2&gt;

&lt;p&gt;We want to show country details, when user clicks on any of the countries. To achieve this, first we need to add an &lt;code&gt;onClick&lt;/code&gt; event handler to every &lt;code&gt;ListGroupItem&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ListGroupItem key={country.name} onClick={() =&amp;gt; setDetails(country)}&amp;gt;
  {country.name}
&amp;lt;/ListGroupItem&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need another state variable, where we can hold the content of the main section. If no country is clicked, main section should be empty. If any of the countries are clicked, then it should show relevant information to that country.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from "react"
import axios from "axios"
import "./App.scss"
import {
  Container,
  Row,
  Col,
  ListGroup,
  ListGroupItem,
  Form,
} from "react-bootstrap"

const App = (props) =&amp;gt; {
  const [countries, setCountries] = useState([])
  const [filtered, setFiltered] = useState([])
  const [search, setSearch] = useState("")
  const [details, setDetails] = useState([])

  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      const response = await axios.get("https://restcountries.eu/rest/v2/all")

      setCountries(response.data)
    }

    fetchData()
  }, [countries])

  useEffect(() =&amp;gt; {
    setFiltered(
      countries.filter((country) =&amp;gt;
        country.name.toUpperCase().includes(search.toUpperCase())
      )
    )
  }, [countries, search])

  const handleSearch = (event) =&amp;gt; {
    setSearch(event.target.value)
  }

  return (
    &amp;lt;Container fluid&amp;gt;
      &amp;lt;Row&amp;gt;
        &amp;lt;Col md="3"&amp;gt;
          &amp;lt;Form&amp;gt;
            &amp;lt;Form.Control
              value={search}
              type="text"
              placeholder="Filter Countries..."
              onChange={handleSearch}
            /&amp;gt;
          &amp;lt;/Form&amp;gt;
          &amp;lt;ListGroup&amp;gt;
            {filtered.map((country) =&amp;gt; (
              &amp;lt;ListGroupItem
                key={country.name}
                onClick={() =&amp;gt; setDetails(country)}
              &amp;gt;
                {country.name}
              &amp;lt;/ListGroupItem&amp;gt;
            ))}
          &amp;lt;/ListGroup&amp;gt;
        &amp;lt;/Col&amp;gt;
        &amp;lt;Col md="9"&amp;gt;
          {details.length === 0 ? (
            ""
          ) : (
            &amp;lt;Container&amp;gt;
              &amp;lt;Row className="justify-content-md-start align-items-start"&amp;gt;
                &amp;lt;Col&amp;gt;
                  &amp;lt;h1&amp;gt;{details.name}&amp;lt;/h1&amp;gt;
                  &amp;lt;p&amp;gt;Capital City: {details.capital}&amp;lt;/p&amp;gt;
                  &amp;lt;p&amp;gt;Population: {details.population}&amp;lt;/p&amp;gt;
                  &amp;lt;h3&amp;gt;Languages&amp;lt;/h3&amp;gt;
                  &amp;lt;ul&amp;gt;
                    {details.languages.map((language) =&amp;gt; (
                      &amp;lt;li key={language.name}&amp;gt;{language.name}&amp;lt;/li&amp;gt;
                    ))}
                  &amp;lt;/ul&amp;gt;
                &amp;lt;/Col&amp;gt;
                &amp;lt;Col&amp;gt;
                  &amp;lt;img
                    src={details.flag}
                    height="auto"
                    width="320px"
                    alt="country flag"
                  /&amp;gt;
                &amp;lt;/Col&amp;gt;
              &amp;lt;/Row&amp;gt;
            &amp;lt;/Container&amp;gt;
          )}
        &amp;lt;/Col&amp;gt;
      &amp;lt;/Row&amp;gt;
    &amp;lt;/Container&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-4.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2FReact-App.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-4.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2FReact-App.png" alt="detail render"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Weather Details
&lt;/h2&gt;

&lt;p&gt;Let's implement a second API to show weather details in the capital. We will use Weatherstack API. In order to use it, we need to have an account. When we login, there is an API access key on the dashboard. We need that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-2.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2FAccount-Dashboard--weatherstack-API.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-2.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2FAccount-Dashboard--weatherstack-API.jpg" alt="api access key"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in the root folder. In this file create a &lt;code&gt;key=value&lt;/code&gt; pair. There shouldn't be any other punctuation marks, including quotes or double quotes.&lt;br&gt;
Also key should be starting with &lt;code&gt;REACT_APP_&lt;/code&gt;. For example my &lt;code&gt;.env&lt;/code&gt; entry looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REACT_APP_WEATHERSTACK_API_KEY=14218xxx555xxxxx78yyy26d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We cannot make the second API as we did before. On the first time, we fetch country data when app starts. It is not dynamic and there is no user interaction. On the other hand, We fetch weather data after user selects a country and we need to set the state and render UI correctly as user expects. It changes on every user input. Therefore, we need to change our approach.&lt;/p&gt;

&lt;p&gt;We will expand what we do on user click and handle everything on a seperate method -&amp;gt; &lt;code&gt;handleSelectCountry&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleSelectCountry = async (country) =&amp;gt; {
    const response = await axios.get(
      `http://api.weatherstack.com/current?access_key=${process.env.REACT_APP_WEATHERSTACK_API_KEY}&amp;amp;query=${country.capital}`
    )

    const weather = response.data.current

    setDetails(
      &amp;lt;Container&amp;gt;
        &amp;lt;Row className="justify-content-md-start align-items-start"&amp;gt;
          &amp;lt;Col&amp;gt;
            &amp;lt;h1&amp;gt;{country.name}&amp;lt;/h1&amp;gt;
            &amp;lt;p&amp;gt;Capital City: {country.capital}&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Population: {country.population}&amp;lt;/p&amp;gt;
            &amp;lt;h3&amp;gt;Languages&amp;lt;/h3&amp;gt;
            &amp;lt;ul&amp;gt;
              {country.languages.map((language) =&amp;gt; (
                &amp;lt;li key={language.name}&amp;gt;{language.name}&amp;lt;/li&amp;gt;
              ))}
            &amp;lt;/ul&amp;gt;
            &amp;lt;h3&amp;gt;Weather in {country.capital}&amp;lt;/h3&amp;gt;
            &amp;lt;p&amp;gt;temperature: {weather.temperature} Celcius&amp;lt;/p&amp;gt;
            &amp;lt;img src={weather.weather_icons[0]} alt="Temp Icon" /&amp;gt;
            &amp;lt;p&amp;gt;Wind Speed: {weather.wind_speed} mph&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Wind Direction: {weather.wind_dir}&amp;lt;/p&amp;gt;
          &amp;lt;/Col&amp;gt;
          &amp;lt;Col&amp;gt;
            &amp;lt;img
              src={country.flag}
              height="auto"
              width="320px"
              alt="country flag"
            /&amp;gt;
          &amp;lt;/Col&amp;gt;
        &amp;lt;/Row&amp;gt;
      &amp;lt;/Container&amp;gt;
    )
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before, we were using details state to store country data. Now, we store the output JSX code. Before we construct the JSX, we also make an asynchronous call to weather API.&lt;/p&gt;

&lt;p&gt;Final Result looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-1.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2Fcountry-thumb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres-1.cloudinary.com%2Fhaj7enwld%2Fimage%2Fupload%2Fq_auto%2Fv1%2Fghost-blog-images%2Fcountry-thumb.png" alt="final result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although a little bit of beautification and customization is still needed, our project is done. I will share my result down below. You can try this part yourself.&lt;/p&gt;

&lt;p&gt;Live demo of this project is available here: &lt;a href="https://country-browser-azure.vercel.app/" rel="noopener noreferrer"&gt;https://country-browser-azure.vercel.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Repository is available here: &lt;a href="https://github.com/afgonullu/country-browser" rel="noopener noreferrer"&gt;https://github.com/afgonullu/country-browser&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I would love to hear your opinions and continue conversation. If you want to get in touch, feel free to follow me and send me a message on twitter &lt;a class="mentioned-user" href="https://dev.to/afgonullu"&gt;@afgonullu&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>tutorial</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why Learn To Code?</title>
      <dc:creator>A Faruk Gonullu</dc:creator>
      <pubDate>Mon, 23 Nov 2020 19:16:57 +0000</pubDate>
      <link>https://dev.to/afgonullu/why-learn-to-code-m6</link>
      <guid>https://dev.to/afgonullu/why-learn-to-code-m6</guid>
      <description>&lt;p&gt;If you get to do one thing for yourself, one investment for your future, it should be learning to code. "Software is eating the world", while giving people the independence they seek. Considering the change in the global landscape, which is accelerated because of the pandemic, you don't want to miss being part of the action.&lt;/p&gt;

&lt;p&gt;I want to discuss the topic in 3 main axes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Intrinsic values and propositions of coding, what are the qualities and opportunities comes with it.&lt;/li&gt;
&lt;li&gt;Current and prospective market conditions, ever increasing demand and resulting supply crisis.&lt;/li&gt;
&lt;li&gt;Shifts happening in the internet era, in modern way of life and their effects on creating wealth and value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's dive into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Intrinsic Qualities of Coding
&lt;/h3&gt;

&lt;p&gt;From the top of my head, I can list out several practical and obvious values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Coding helps with creativity, math and problem solving skills. These can overall lead to a more successful professional and personal life.&lt;/li&gt;
&lt;li&gt;Coding helps with understanding algorithms, patterns and organizations. This can lead to better planning skills, organizing thoughts, even to better academic performance.&lt;/li&gt;
&lt;li&gt;Coding doesn't discriminate. Everyone can learn it. There is no threshold to pass, there are no requirements. Everyone can do it.&lt;/li&gt;
&lt;li&gt;Coding is another language. In a way, it is the literacy of future. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apart from all these, maybe the most important thing to talk about is that it may be the most asymmetric leverage towards financial freedom in human history. &lt;/p&gt;

&lt;p&gt;Coding makes it possible to create something for yourself, by yourself. You need neither manpower, nor capital. Not anymore. It makes it easier to work on passion projects / side hustles. You can create apps without too much investment, whether it is time or money. And most importantly, when you build something, you can sell it infinitely. &lt;/p&gt;

&lt;p&gt;The possibility to create, have ownership, market and sell your products on your own terms is the game changer. Coding gives you the opportunity to be financially free.&lt;/p&gt;

&lt;h3&gt;
  
  
  Market Reality
&lt;/h3&gt;

&lt;p&gt;There is a also a huge demand for software in the market, and it will only increase in the future. Software is not even an industry itself, but it is an essential part of every industry. Every industry, every job, every process, every task can be digitalized and be carried out by a software or a robot. This digital transformation is happening for some time now, and it will continue in a growing pace in the future. Most of the work, if not all, will someday be carried out by autonomous robots.&lt;/p&gt;

&lt;p&gt;Some of the industries, jobs and tasks have been around for hundred years. People have always adapted advancements in science and technology into their practice. Science and technology shaped the definitions of jobs. Same is true for software. Marc Andreessen, co-founder of Netscape among other things, said: "Software is eating the world". I would highly suggest everyone to read his article here. &lt;/p&gt;

&lt;p&gt;Which also brings us to the next topic:&lt;/p&gt;

&lt;h3&gt;
  
  
  Shifting Medium
&lt;/h3&gt;

&lt;p&gt;Internet, especially social networks changed how we live. Not only that, they also transformed the way we think, the way we feel and everything else. The abundance of information and content, the destruction of entry barriers, the accessible and affordable infrastructure, etc., all of these things calls for us to create value and share it with the world.&lt;/p&gt;

&lt;p&gt;Big percentage of information and value is created on the internet. Businesses are moving to internet. There are so many information, ideas, opinions, tv shows, movies, music, whatever you can think of... There is a huge abundance in everything you imagine. So much going on, even there is a need for people to tell us what to choose. There are curators, influencer, youtubers, streamers, etc. The shift has already happened! We are just lagging.&lt;/p&gt;

&lt;p&gt;High value / High impact creation doesn't require big organizations and big investments anymore. Every six months or so, We hear another small team creating a multi billion dollar company. Traditional jobs are not creating much value anymore. 9-5 job hours are quickly becoming obsolete. People, calling themselves "Digital Nomads" are working over the internet, without a fixed location. More and more companies are adjusting their teams to remote working. Even future of money is shifting to a decentralized currency, according to many advocates of Bitcoin.&lt;/p&gt;

&lt;p&gt;Would you want to miss the train, or embrace the possibility to create 100x, 1000x leveraged value on the internet, through code?&lt;/p&gt;

&lt;p&gt;(Cover Photo by Oskar Yildiz / Unsplash)&lt;/p&gt;

</description>
      <category>career</category>
      <category>startup</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
