<?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: cigwe416</title>
    <description>The latest articles on DEV Community by cigwe416 (@cigwe416).</description>
    <link>https://dev.to/cigwe416</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%2F574533%2Feee47527-8c4a-42e1-b628-627e634f49e0.jpeg</url>
      <title>DEV Community: cigwe416</title>
      <link>https://dev.to/cigwe416</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cigwe416"/>
    <language>en</language>
    <item>
      <title>Fix CORS Error (JavaScript)</title>
      <dc:creator>cigwe416</dc:creator>
      <pubDate>Thu, 04 Mar 2021 18:01:42 +0000</pubDate>
      <link>https://dev.to/cigwe416/fix-cors-error-javascript-1na2</link>
      <guid>https://dev.to/cigwe416/fix-cors-error-javascript-1na2</guid>
      <description>&lt;h1&gt;
  
  
  What is CORS?
&lt;/h1&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkz3x3zf6oa75ow4gyyr8.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkz3x3zf6oa75ow4gyyr8.png" alt="cors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any other origins (domain, scheme, or port) than its own from which a browser should permit loading of resources - &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This definition might seem confusing so let me try to explain it in simpler terms.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;CORS&lt;/strong&gt; is a browser policy that allows you to make requests from one website to another which by default is not allowed by another browser policy called Same Origin Policy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is an error that is mostly addressed from the backend of an API.&lt;br&gt;
The problem here is when you are trying to call a public API without the CORS error being fixed and you can't reach the developers that developed the API.&lt;/p&gt;

&lt;p&gt;In this tutorial, I'll be showing you how to by-pass CORS errors using Vanilla Javascript when you are in such a situation.&lt;/p&gt;

&lt;p&gt;The API we are going to be using is a &lt;code&gt;Quote Generator&lt;/code&gt; API.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://api.forismatic.com/api/1.0/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In other to get list of Quotes, we need to append this to the base URL&lt;/p&gt;

&lt;p&gt;&lt;code&gt;?method=getQuote&amp;amp;lang=en&amp;amp;format=json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So the full URL becomes;&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;code&gt;http://api.forismatic.com/api/1.0/?method=getQuote&amp;amp;lang=en&amp;amp;format=json&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;In other to make the API call, we need to create a Javascript file and call the endpoint.We would be using the &lt;code&gt;fetch&lt;/code&gt; API.&lt;/p&gt;

&lt;p&gt;This would look like this;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Get quote from API&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getQuote&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;apiUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://api.forismatic.com/api/1.0/?method=getQuote&amp;amp;lang=en&amp;amp;format=json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// On load&lt;/span&gt;
&lt;span class="nf"&gt;getQuote&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;If you run this code in your browser and open up your console, you should see the error below;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4y2oq9tqyhwedkmpequ.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4y2oq9tqyhwedkmpequ.png" alt="Screen Shot 2021-03-04 at 6.39.46 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In other to fix this error, add the following lines of code;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Get quote from API&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getQuote&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;proxyUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://cors-anywhere.herokuapp.com/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;apiUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://api.forismatic.com/api/1.0/?method=getQuote&amp;amp;lang=en&amp;amp;format=json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;proxyUrl&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// On load&lt;/span&gt;
&lt;span class="nf"&gt;getQuote&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This URL &lt;code&gt;https://cors-anywhere.herokuapp.com/&lt;/code&gt; is also a public API that was created by someone to fix the CORS error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;N.B&lt;/strong&gt;: You might still get some errors on your console even after following the steps I just showed.If this happens, go to this URL &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   `https://cors-anywhere.herokuapp.com/corsdemo`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;and follow the instructions there.&lt;/p&gt;

&lt;p&gt;Thanks for taking your time to read this Article. Your feedback and comment is highly welcomed.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>cors</category>
      <category>fetch</category>
    </item>
    <item>
      <title>Hosting your React Application on Github</title>
      <dc:creator>cigwe416</dc:creator>
      <pubDate>Mon, 01 Mar 2021 17:08:33 +0000</pubDate>
      <link>https://dev.to/cigwe416/hosting-your-react-application-on-github-1g64</link>
      <guid>https://dev.to/cigwe416/hosting-your-react-application-on-github-1g64</guid>
      <description>&lt;p&gt;In this article, I'll guide you on how to host your frontend applications using GitHub's free hosting service.&lt;/p&gt;

&lt;h4&gt;
  
  
  N.B They are other free hosting services like Netlify, Heroku, Vercel, and so on but today, our focus would be using GitHub.
&lt;/h4&gt;

&lt;p&gt;The steps are as follows:&lt;br&gt;
First you need to create a react application and push to Github. In this article, we would be building a simple input element inspired by Google's Material Design.&lt;/p&gt;

&lt;p&gt;Create the react application you want to host using the steps below&lt;br&gt;
a. &lt;code&gt;npx create-react-app app-name&lt;/code&gt; where app-name is the name of your application. Feel free to choose whatever name you want.&lt;br&gt;
b. Open the application in your preferred code editor. I'll be using vscode.&lt;br&gt;
copy and paste the following code in your App.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;className=&lt;/span&gt;&lt;span class="s"&gt;"input__element"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;autocomplete=&lt;/span&gt;&lt;span class="s"&gt;"off"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;Your name&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  N.B Delete the header tag and all of it's content before pasting the code you copied.
&lt;/h4&gt;

&lt;p&gt;Your App.js 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;import React from 'react';
import './App.scss';

function App() {
    return (
     &amp;lt;div className="app"&amp;gt;
      &amp;lt;form&amp;gt;
       &amp;lt;div className="input__element"&amp;gt;
    &amp;lt;input type="text" autocomplete="off" name="name" /&amp;gt;
     &amp;lt;label&amp;gt;Label&amp;lt;/label&amp;gt;
       &amp;lt;/div&amp;gt;
       &amp;lt;/form&amp;gt;
      &amp;lt;/div&amp;gt;
   );
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the App.scss import.&lt;/p&gt;

&lt;p&gt;Copy the scss code below and paste in you App.scss file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$primary-color:#296EF9;

.app{
  display: flex;
  justify-content: center;
  align-items: center;
form{
  margin-top: 300px;
.input__element {
  position: relative;
  width: 100%;
  margin-bottom: 1rem;
  label{
    position: absolute;
    font-size: 1rem;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    background-color: white;
    color: #909090;
    padding: 0 0.3rem;
    margin: 0 0.5rem;
    transition: .1s ease-out;
    transform-origin: left top;
    pointer-events: none;
    font-size: 14px;
   }
   input {
    font-size: 1rem;
    outline: none;
    border: 1px solid #f3f1f1;
    border-radius: 5px;  
    padding: 1rem 0.8rem;
    color: #909090;
    transition: 0.1s ease-out;
    width: 100%;
     &amp;amp;:focus {
      border-color: $primary-color;  
     }
     &amp;amp;:focus + label {
      color: $primary-color;
      top: 0;
      transform: translateY(-50%) scale(.9);
     }
   }
 }
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can easily setup scss in your react app by running:&lt;br&gt;
&lt;code&gt;npm install --save node-sass&lt;/code&gt; and renaming your .css files to .scss&lt;/p&gt;

&lt;p&gt;Open your teminal and run &lt;code&gt;npm start&lt;/code&gt;. This command should automatically open your application in your default browser and you should see something like the screenshoot below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x0qg0Kdh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpyyxoc452jatvbu9mdd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x0qg0Kdh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpyyxoc452jatvbu9mdd.png" alt="Screen Shot 2021-03-01 at 5.32.24 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After creating your application, you need to push it to GitHub.&lt;/p&gt;

&lt;p&gt;Now the interesting part.&lt;/p&gt;

&lt;p&gt;Open your terminal and run this command:&lt;br&gt;
  &lt;code&gt;npm install gh-pages --save-dev&lt;/code&gt;. This would install the gh-pages package as a dev dependency.&lt;/p&gt;

&lt;p&gt;Open your package.json file and add the following:&lt;br&gt;
 Add &lt;br&gt;
"homepage":"&lt;a href="http://your-github-username.github.io/repo-"&gt;http://your-github-username.github.io/repo-&lt;/a&gt; &lt;br&gt;
   name",&lt;br&gt;
 as one of the properties&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to your scripts section and add these lines too:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      "predeploy":"npm run build",
      "deploy":"gh-pages -d build"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, run &lt;code&gt;npm run deploy&lt;/code&gt; to deploy your application.&lt;br&gt;
   Your application is finally live and can be access over the internet.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;This is my first technical article. Feel free to comment on it, give feedback on where I can improve on and also, like this article if it was helpful.&lt;/p&gt;

</description>
      <category>github</category>
      <category>react</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
