I can make sure "console.log("client.ImageNames", client.ImageNames);" works and get the result like below. client.ImageNames
- (3) [“image1.png", "image2.png", "image3.png"]
- 0: "image1.png"
1: "image2.png"
2: "image3.png"
length: 3
proto: Array(0)
- 0: "image1.png"
1: "image2.png"
2: "image3.png"
length: 3
proto: Array(0)
However, when I tried to loop the array, it worked first, but when I reloaded, I got "Cannot read property 'forEach' of undefined". I guess useEffect hook is related to this issue but I have no idea how to fix it.
import { useParams } from "react-router-dom";
import React, { useState, useEffect } from "react";
import axios from "axios";
const ClientDetail = () => {
const { id } = useParams();
const [client, setClients] = useState({});
useEffect(() => {
axios.get(`http://localhost:8080/clients/${id}`).then((response) => {
setClients(response.data);
});
}, []);
useEffect(() => {
setClients(client);
console.log("client.ImageNames", client.ImageNames);
client.ImageNames.forEach((element) => {
console.log("element : " + element);
});
}, [client]);
return (
<div>
{client.name}
</div>
);
};
export default ClientDetail;