From debbf2808dd4548fb6e7aeafbfaaf188fc98dc45 Mon Sep 17 00:00:00 2001 From: Jaylou Rasonabe <100252318+JlordS32@users.noreply.github.com> Date: Sun, 14 May 2023 17:39:12 +1000 Subject: [PATCH] Update 25_custom_hooks.md --- 25_Custom_Hooks/25_custom_hooks.md | 86 +++++++++++++++++------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/25_Custom_Hooks/25_custom_hooks.md b/25_Custom_Hooks/25_custom_hooks.md index fda64e4..3e31420 100644 --- a/25_Custom_Hooks/25_custom_hooks.md +++ b/25_Custom_Hooks/25_custom_hooks.md @@ -29,55 +29,65 @@ import React, { useState, useEffect } from 'react' import axios from 'axios' import ReactDOM, { findDOMNode } from 'react-dom' -const Country = ({ country: { name, flag, population } }) => { - return ( -
-
- {name} -
-

{name.toUpperCase()}

-
-

- Population: - {population} -

+const Country = ({ country: { + name: { + common: country_name + }, + flags: { + png: flag_png, + alt: flag_alt + }, + population +}}) => { + return ( +
+
+ {flag_alt}/ +
+

{country_name}

+
+

+ + Population: {population.toLocaleString()} + +

+
-
- ) + ) } const App = (props) => { - // setting initial state and method to update state - const [data, setData] = useState([]) + const [data, setData] = useState([]) - useEffect(() => { - fetchData() - }, []) + useEffect(() => { + fetchData(); + }, []) - const fetchData = async () => { - const url = 'https://restcountries.eu/rest/v2/all' - try { - const response = await fetch(url) - const data = await response.json() - setData(data) - } catch (error) { - console.log(error) - } - } + const fetchData = async () => { + const url = 'https://restcountries.com/v3.1/all' + try { + const response = await fetch(url); + const data = await response.json(); + setData(data); + console.log(data); + } catch (error) { + console.log(error); + } + } - return ( -
+ return ( +

Fetching Data Using Hook

Calling API

-

There are {data.length} countries in the api

-
- {data.map((country) => ( - - ))} -
+

There are {data.length} countries in the api

+
+ {data.map((country) => ( + + ))} +
-
+
) }