Skip to content

Instantly share code, notes, and snippets.

@MarwanShehata
Last active April 19, 2024 05:15
Show Gist options
  • Save MarwanShehata/b3e905411659ae2ff7f61b71ad7a6e3c to your computer and use it in GitHub Desktop.
Save MarwanShehata/b3e905411659ae2ff7f61b71ad7a6e3c to your computer and use it in GitHub Desktop.
The `ignore` variable in your code serves as a flag to prevent state updates after an asynchronous operation (in this case, fetching country data) has completed, especially when the component might have been unmounted or the state might have been upd
import {
createContext,
memo,
useCallback,
useContext,
useEffect,
useId,
useLayoutEffect,
useMemo,
useReducer,
useRef,
useState,
useSyncExternalStore,
} from 'react';
import './App.css';
import React from 'react';
export default function App() {
const [countryCode, setCountryCode] = useState('EG');
const [data, setData] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
setIsLoading(true);
let ignore = false; // We have to ignore any responses from still responses, because of the async nature of the async request we dont know when this will resolve so if it resolves after we already requested another country we don't want to update our state
const fetchCountryData = async () => {
try {
const response = await fetch(
`https://restcountries.com/v2/alpha/${countryCode}`
);
if (!response.ok) {
throw new Error('Network response wasnt ok');
}
const data = await response.json();
if (ignore === false) {
setData(data);
setError(null);
setIsLoading(false);
}
console.log(data);
} catch (error) {
if (ignore === false) {
setData(null);
setError(error);
setIsLoading(true);
}
console.error(`there was an error`);
console.error(error);
}
};
fetchCountryData();
return () => {
ignore = true;
};
}, [countryCode]);
const handleChange = (e) => {
setCountryCode(e.target.value);
};
return (
<section>
<header>
<h1>Country Info:</h1>
<label htmlFor='country'>Select a country:</label>
<div>
<select id='country' value={countryCode} onChange={handleChange}>
<option value='AU'>Australia</option>
<option value='CA'>Canada</option>
<option value='CN'>China</option>
<option value='FR'>France</option>
<option value='DE'>Germany</option>
<option value='IN'>India</option>
<option value='JP'>Japan</option>
<option value='MX'>Mexico</option>
<option value='GB'>United Kingdom</option>
<option value='US'>United States of America</option>
</select>
{isLoading && <span>Loading...</span>}
{error && <span>{error.message}</span>}
</div>
</header>
{data && (
<article>
<h2>{data.name}</h2>
<table>
<tbody>
<tr>
<td>Capital:</td>
<td>{data.capital}</td>
</tr>
<tr>
<td>Region:</td>
<td>{data.region}</td>
</tr>
<tr>
<td>Population:</td>
<td>{data.population}</td>
</tr>
<tr>
<td>Area:</td>
<td>{data.area}</td>
</tr>
</tbody>
</table>
</article>
)}
</section>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment