Why won't the <li> element be returned in the loop
Unanswered
Schneider’s Smooth-fronted Caima… posted this in #help-forum
Schneider’s Smooth-fronted CaimanOP
import axios from 'axios'
function page() {
return (
<div>
<ul>
{
axios.get("http://localhost:3000/api/pokemons")
.then((response) => {
const data = response.data.results
console.log(data)
// Display the data
data.map((pokemon, k) => {
return (
<li key={k}>{pokemon.name}</li>
)
})
})
.catch(err => {
console.log(err)
})
}
</ul>
</div>
)
}
export default pageThis is my code. I'm trying to return the <li> when the data array is looped. Why is it not displaying on the page?
No error, just blank
Edit: When I added
"use client"I go this error:
Error: Hydration failed because the initial UI does not match what was rendered on the server.6 Replies
@Schneider’s Smooth-fronted Caiman jsx
import axios from 'axios'
function page() {
return (
<div>
<ul>
{
axios.get("http://localhost:3000/api/pokemons")
.then((response) => {
const data = response.data.results
console.log(data)
// Display the data
data.map((pokemon, k) => {
return (
<li key={k}>{pokemon.name}</li>
)
})
})
.catch(err => {
console.log(err)
})
}
</ul>
</div>
)
}
export default page
This is my code. I'm trying to return the <li> when the data array is looped. Why is it not displaying on the page?
No error, just blank
Edit: When I added
js
"use client"
I go this error:
Error: Hydration failed because the initial UI does not match what was rendered on the server.
you can't do that inside the jsx. If you can achieve that by doing this:
I would rather reading basics before building such "complex" applications
import axios from "axios";
import {useState, useEffect} from "react";
export default function page(){
const [data, setData] = useState();
useEffect(() => {
axios.get("http://localhost:3000/api/pokemons").then((res) => setData(res.data.results)).catch(err => console.log(err));
}, []);
return (
<div>
<ul>
{data.map((pokemon, k) => {
return <li key={k}>{pokemon.name}</li>;
})}
</ul>
</div>
)
}I would rather reading basics before building such "complex" applications
Schneider’s Smooth-fronted CaimanOP
Thanks a lot.
I'll go revisit react docs. Don't know much about useEffect
I'll go revisit react docs. Don't know much about useEffect
Schneider’s Smooth-fronted CaimanOP
Thanks a lot
@Coffee Coke you can't do that inside the jsx. If you can achieve that by doing this:
tsx
import axios from "axios";
import {useState, useEffect} from "react";
export default function page(){
const [data, setData] = useState();
useEffect(() => {
axios.get("http://localhost:3000/api/pokemons").then((res) => setData(res.data.results)).catch(err => console.log(err));
}, []);
return (
<div>
<ul>
{data.map((pokemon, k) => {
return <li key={k}>{pokemon.name}</li>;
})}
</ul>
</div>
)
}
I would rather reading basics before building such "complex" applications
if you don't have any more questions mark this as an sollution (right click > Apps > Mark Solution)
@Schneider’s Smooth-fronted Caiman Thanks a lot
you are welcome