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-dom
And 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 V4 for the design 👍
$ npm install bootstrap
To 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-dom
Link
which we will use to send the country code (alpha3Code
) via the URL. -
CountryDetails
: Is the component that we will render via thereact-router-dom
'sRoute
and will be receiving the country code (alpha3Code
) via the URL.This is actually the id of the country (example:
/ESP
for Spain,/FRA
for 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
.
The simplest way to define a component in React is to write a JavaScript function aka function component. The navbar should be displaying the title of the LAB - WikiCountries.
This component should render a list of Link
s that are used to trigger the browser URL change. Click on a Link
component will then activate the corresponding Route
showing the country details component.
Now that our list of countries is ready, we should create the CountryDetails
component. CountryDetails
displays the country details as per the link that we clicked. This component should be dynamically displayed/rendered with the <Route />
using the render
prop:
// Example
<Route render={(props) => <SomeComponent {...props} otherProp={someData} > } />
You should create only 1 <Route />
for the CountryDetails
component.
Components rendered by the Route
receive special props
(match
, location
and history
) passed by the react-router-dom
. We can use this props to obtain the information coming from the browser's URL bar, for example, the alpha3Code
code of the country. For a reminder on how to set up and access the URL parameters with React Router check this example.
NOTE: For the small picture of the flag, you can use the alpha2Code
and embed it in the URL as shown below:
- France: https://www.countryflags.io/FR/flat/32.png
- Germany: https://www.countryflags.io/DE/flat/32.png
- Brazil: https://www.countryflags.io/BR/flat/32.png
- etc.
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 Switch & the Route rendering the CountryDetails should go here */}
</div>
</div>
</div>
Our App.js
application should have its own state with a state variable countries
holding the data coming from the countries.json
file.
The state should be set only once, after the component is initially rendered to the DOM using the useEffect()
Hook.
Instead of relying on the static data coming from a json
file, let's do something more interesting and get out the data from an actual API.
Let's make a GET
request to the URL https://restcountries.eu/rest/v2/name/all and 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.
The request should happen first thing when the application loads, therefore think about when and from where we should make the request to the API.
Note: The sturcture of the objects/documents returned by the RestCountries API may be different than the one you have in the countries.json
file. This means that you may need to update your components to correctly access the values from the objects.
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 endpoint using the country's alpha3Code
. Example:
-
United States: https://restcountries.eu/rest/v2/alpha/USA
The effect should run after the initial render and each time the URL parameter with the alpha3Code
changes.
Happy coding! ❤️