After spending too much time on GitHub, you found a JSON dataset of countries, and you decided to use it to create your Wikipedia of countries!
-
Fork this repo
-
Clone this repo
-
Open the LAB and start:
$ cd lab-wiki-countries $ npm install $ npm start
-
Upon completion, run the following commands:
git add . git commit -m "done" git push origin master
-
Create a Pull Request so that your TAs can check your work.
Clean the App.js component so that it has the following structure:
// src/App.js
import "./App.css";
function App() {
return <div className="App"></div>;
}
export default App;Remember to install the React Router:
$ npm install react-router-domAnd setup the router in your src/index.js file:
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter as Router } from 'react-router-dom';
ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById('root')
);
// comment skipped to stay organized
reportWebVitals();We will use Bootstrap for the design 👍
$ npm install bootstrapTo make the Bootstrap styles available in the entire app, import the stylesheet in index.js:
// src/index.js
import 'bootstrap/dist/css/bootstrap.css';In this iteration, we will focus on the general layout. Before you start, inside the src folder, create the components folder. There you will create at least 3 components:
-
Navbar: Displaying the basic navbar with the LAB name -
CountriesList: Displays the list of links with the country names. Each link should be areact-router-domLinkwhich we will use to send the country code (alpha3Code) via the URL. -
CountryDetails: This is the component that we will render via thereact-router-dom'sRoute, and it should receive the country code (alpha3Code) via the URL.This is the id of the country (example:
/ESPfor Spain,/FRAfor France).
To help you with the structure of the components, we gave you an example of a page inside example.html.
If you want to style it, refresh your memory on Bootstrap in the docs or check out how we approached styling in the example.html.
Create a navbar component that displays the title LAB - WikiCountries.
This component should render a list of Links, each having the country's alpha3Code embedded in the URL. Click on a Link should render the country details component.
Now that our list of countries is ready, we should create the CountryDetails component. This component should receive the array of countries as a prop. Here's a reminder on how to do this:
// Example
<Route path="/:id" element={ <SomeComponent someData={someData} /> } />The alpha3Code of the country will be available throught the URL parameters. To access the URL parameters, from the browser's URL bar, use the React Routers hooks useParams. For a reminder on setting up and accessing the URL parameters with React Router, check this example.
NOTE: For the small picture of the flag, you can use the lowercased alpha2Code and embed it in the URL as shown below:
- France: https://flagpedia.net/data/flags/icon/72x54/fr.png
- Germany: https://flagpedia.net/data/flags/icon/72x54/de.png
- Brazil: https://flagpedia.net/data/flags/icon/72x54/br.png
- etc.
App should have a state variable countries holding the data coming from the src/countries.json file. The data from the state variable countries should then be passed to the CountriesList component as a prop.
Once done creating the components, the structure of elements that your App.js will render should look somewhat like this:
// ...
<div className="App">
<Navbar />
<div className="container">
<div className="row">
<CountriesList countries={countries} />
{/* React-Router Route rendering the CountryDetails should go here */}
</div>
</div>
</div>
// ...Instead of relying on the static data from a json file, let's do something more interesting and get the data from an actual API.
In App.js, make a GET request to the URL https://ih-countries-api.herokuapp.com/countries. Use the data returned from the response as the list of the countries. You can use either fetch or axios to make the request.
You should use the useEffect() hook to set an effect that runs only once and makes a request to the API. Once you receive the response data, save it in a state variable. The request should happen first thing when the application loads.
Using the useEffect hook set an effect in the CountriesDetails component. The effect should make a request to the RestCountries API and fetch the data for the specific country. You can construct the request URL using the country's alpha3Code. Example:
-
United States: https://ih-countries-api.herokuapp.com/countries/USA
The effect should run after the initial render, and each time the URL parameter with the alpha3Code changes.
Happy coding! ❤️

