We will use the React useEffect and useState hooks and Axios for this project
Basic functionality: we want our app to request data about cryptocurrencies from the Coingecko API and show this data in an attractive overview. With the app we can also search for a currency and display the information in the browser. In addition, the currency prices are updated live whenever the page is refreshed.
A simple example looks like this:
- Create a new project on your local computer using
npx create-react-app project-cryptocurrency - Delete the code from the App.css and use index.css for the initial, general CSS
- Delete the code in the
App.jscomponent between<div className="App">and the closing</div> - In the
return()ofApp.js, add<h1>Search Currency</h1>and run your code withnpm startto test the component (you should see the text "Search Currency" as title on the page) - Create a search input form in
App.jsthat will be used to find a currency by name, with anonChange()handler (see in the second part for the description of the handler function) - Create a
componentsfolder inside yoursrcfolder - Create a functional component Coin.js inside this file (remember the React syntax!) and a
Coin.cssfile. This means we use a separate CSS file for the component (this article discusses how to organise your code files, and one option is having one separate CSS file for each React component instead of one general CSS file) - In
Coin.jscreate a<div>where you will use the values from the API to be shown in the overview (see example image above) - From the Coingecko API, we use seven values: name, symbol, current_price, total_volume, image,price_change_percentage_24h, and market_cap
- In
App.js, useuseEffect()to fetch the data from this API - You can also use
axiosfor fetching data. To do this, you have to installaxiosand import axios inApp.js(you can find out how to do that). Instead of thefetch("")syntax, you have to useaxios.get("")to get the data. You can read more about axios in here - Create two hooks, one with
coinsandsetCoinsand set the state with an empty array as default; the other hook is withsearchandsetSearch, and the state with an empty string - Create a
handleChange()function that uses thesetSearchhook to setsearchas the target value when the event is triggered (i.e. when a search text is typed) - Create a function to filter the data for the currency that is entered into the search field, and pass the result into a variable called
filteredCoins - In the
return()ofApp.js, create adivwith class namecoin-app - In this 'div', map over
filteredCoinsand return theCoin.jscomponent, passing in all the seven values as props, pluskey={coin.id}(React requires a key when you usemap()) - After displaying all data, you can use both
index.cssandCoin.cssto design and layout the app to your own taste
If you would like to build this app using more values from the API, just add more columns in the layout and fill the columns with data
NOTE: You can use this extension ES7 React/Redux/GraphQL/React-Native snippets in your VSCode to create a component automatically using the command rface
If you want to download a project on your local machine, do not fork it but clone the repo locally, on your computer. After that, create a new repo in your own GitHub account with exactly the same project name, and link the local repo to the remote repo in your GitHub account (see below). Why should you clone and not fork? It will show the project as your own project and not a fork of someone else's project. You can use it as a project for your portfolio.
You can connect a local project to a new, empty GitHub repo as follows. It is very good to know this so that you can start a project locally and afterwards link it with a remote GitHub repo.
If you clone the project without forking it, you will have to change the 'remote origin' repository after cloning. Check the remote of your local project using git remote -v.
To link your local project to your own GitHub repo, you need to change the remote origin. Have a look at this article: https://devconnected.com/how-to-change-git-remote-origin/. With git remote -v you can again check if remote origin has been reset and now shows the name of your GitHub account.
