export default function Weather({destiny}) {
const [location, setLocation] = useState('')
const [dataExt, setDataExt] = useState({})
const url_ext = `https://api.openweathermap.org/data/2.5/forecast?q=${destiny}&units=metric&appid="here goes appi id"`
var list = []
const weatherExt = () => {
axios.get(url_ext).then((responseExt) => {
setDataExt(responseExt.dataExt)
list = responseExt.data.list
})
setLocation('')
}
The idea is that when I want to render the result inside a {variable ? ()=>weatherExt() : null}, this function return an html component with results, but I have the following error using the code above: "Warning: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it."
How can I do to best render results returning an html element with a list without having de error render.
Thanks in advance.
2
Answers
I’m assuming you are calling this function directly in the body of your function.
Remember that when you call setFunction from useState (in your case it is the "setLocation" function) your component will be rerender.
See, when you rerender the component, it again creates your function and the function body, in your case -> the weatherExt() function, and this function again causes the component to be rerender.
React say that you are calling the function that caused the re-render and the re-render calls your function again.
Your solution is the useEffect hook.
Try this
And your component should look like this
You could do something like this to get an optimized output:
Important things to note here:
list = responseExt.data.list
should be avoided. Instead use a separate state for that, i.e. useconst [list, setList] = useState([])
and then update this state withsetList()
inside yourweatherExt
function.weatherExt
function insideuseEffect
hook such that it will get executed only once after the component has mounted, that is: