ive tried to just add another fetch to see if it would work but it hasn’t. can anyone explain this issue so i get something out of this.
<ul className="">
{ async function fetchURL() {
const res = await axios.get(ability.ability.url);
const data = res.data;
setabilityDes(data)
pokedata.abilities.map( async (ability, index) => (
<li key={index} className="inline px-3" > {ability.ability.name}
<p>{data.effect}</p>
</li>
))}}
</ul>
this is the error i am getting
‘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.
ul
div
div
div
div
PokemonData@http://localhost:5173/src/pages/PokemonData.jsx?t=1711809411667:23:20
RenderedRoute@http://localhost:5173/node_modules/.vite/deps/react-router-dom.js?v=45fa90fa:3563:7
Routes@http://localhost:5173/node_modules/.vite/deps/react-router-dom.js?v=45fa90fa:3997:7
Router@http://localhost:5173/node_modules/.vite/deps/react-router-dom.js?v=45fa90fa:3945:7
BrowserRouter@http://localhost:5173/node_modules/.vite/deps/react-router-dom.js?v=45fa90fa:4682:7
App’
ive also tried make a new component for just the ability section but i kept running into errors so i thought this would be more efficient
2
Answers
The structure of your query is not correct. There are a lot of ways to fix this problem, but essentially you need to separate the data call from the component display instead of trying to do both all at once.
This is usually done with React Hooks, and there are numerous examples out there of how to accomplish this. Like this one.
The thing to notice about that approach is that it uses the
useState
anduseEffect
hooks to separate the problem into several parts. This approach has the benefit of being reusable and scalable, as it can be easily adjusted to solve any data-fetching problem.So in the example given in the link I posted, the solution is broken into different pieces:
Generally speaking, you’re going to want to separate your concerns as much as you can when computer programming. If you’re trying to grab state, set state, and render state all in one big chunk of code, it’s probably time to slow down and re-think your approach to handle each problem one at a time.
Try re-writing your component in something closer to this form and ask again and I’m glad to help you get where you’re going.
You don’t use functions and logic codes in jsx section!
change your code to this:
for learn more about this codes go to react documents