The effect ends up in an endless re-render loop, it keeps making requests to the url. Basically what I have is an API call to fetch countries. Then based on a filter input, it renders the countries that match the search. If there is only one result, it should also fetch and display weather information of the capital.
The only time I want to re-render the weather is when the value of the country changes. Somehow I can't get it to work..
let filtered = countries.filter(country => country.name.toUpperCase().includes(filter.toUpperCase()))
const [weather, setWeather] = useState([])
useEffect(() => {
if (filtered.length === 1) {
const country = filtered[0]
const api_key = process.env.REACT_APP_API_KEY
const url = `http://api.weatherstack.com/current?access_key=${api_key}&query=${country.capital}`
axios.get(url)
.then(response => {
setWeather(response.data.current)
})
}
}, [filtered])