[英]I am unable to access Json data and display it in table using axios in React
我正在嘗試使用掛鈎訪問 json 數據並將其顯示在
標簽,但它顯示錯誤-“TypeError:states.map 不是函數”。我還使用 Array.from() 將我的 json 轉換為數組,它沒有顯示任何錯誤,但也沒有顯示任何內容。
var rows=[{}];
export default function Mainpage() {
const [states, setstate] = useState([]);
const getLatestJSPost = () => {
const API_URL = "http://localhost:8080/Displaydo";
axios
.get(API_URL)
.then((response) => {
// rows = Array.from(response.data);
console.log(response.data);
setstate(response.data)
})
};
useEffect(() => {
getLatestJSPost();
}, [])
return (
<div>
<p>Hello</p>
{ states.map((ndata) => {
return <p key={ndata.doc_id}> {ndata.doc_id} </p>
})}
</div>
)
}
conole.log(response.data) 看起來像console.log(response.data) 的 Output
你可以這樣做:
<div>
<p>Hello</p>
{states.length>0 && states.map((ndata) => {
return <p key={ndata.doc_id}> {ndata.doc_id} </p>
})}
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.