32
loading...
This website collects cookies to deliver better user experience
https://www.countryflags.io/us/flat/64.png
. This was a problem because my data didn't identify countries by their two letter codes. Another problem I ran into is that the server of countryflags.io went down half way through the project. So to fix these problems, I created my own API that allows users to get a country's flag by the country's name, its two letter code (ISO Alpha 2 code), its three letter code (ISO Alpha 3 code), and its UN Code. The codes for each country can be found at countryflagsapi.com. Here are a few example endpoints import React, { useState } from 'react'
const [flagURL, setFlagURL] = useState('https://countryflagsapi.com/png/cuba')
const [identifier, setIdentifier] = useState('')
const handleButtonClick = () => {
// can use https://countryflagsapi.com/svg/ here too
const url = 'https://countryflagsapi.com/png/'
setFlagURL(url + identifier)
}
<div style={{ marginBottom: '20px' }}>
<input
name="country"
type="text"
onChange={(e) => setIdentifier(e.target.value)}
value={identifier}
placeholder="Enter a country name, UN Code, ISO Alpha-2, or ISO Alpha-3 Code"
/>
<button onClick={handleButtonClick}>Get Flag</button>
</div>
<img src={flagURL} alt="flag" />