[英]React useState sets response from database but when I try to access the data[0].listing_reviews it returns an undefined error
I need some help understanding why my data can be accessed at index[0] but as soon as I try to access a key on it, it returns an undefined error.我需要一些帮助来理解为什么可以在索引 [0] 访问我的数据,但是一旦我尝试访问它上面的键,它就会返回一个未定义的错误。 The initial state of my data is an empty array.
我数据的初始 state 是一个空数组。 I've confirmed that what I get back from the response is indeed an array of length 1 with an object inside of it.
我已经确认我从响应中得到的确实是一个长度为 1 的数组,其中包含一个 object。
App.jsx应用程序.jsx
import React, { useState, useEffect } from 'react';
import Reviews from './Reviews.jsx';
import axios from 'axios';
const ReviewsApp = () => {
const [listingData, setlistingData] = useState([]);
useEffect(() => {
let id = Math.floor(Math.random() * 100) + 1 // Random Test ids
axios
.get(`listings/${id}/reviews`)
.then(res => setlistingData(res.data))
.catch(err => console.log(err));
}, []);
return (
<div>
<Reviews listingData={listingData}/>
</div>
);
};
export default ReviewsApp;
Review.jsx评论.jsx
import React, { useState } from 'react';
const Reviews = ({ listingData }) => {
return (
<div>
{console.log(listingData[0].listing_reviews)}
</div>
)
}
export default Reviews;
So right now, if I console.log(listingData[0]) it works no problem and returns an object with the following structure:所以现在,如果我使用 console.log(listingData[0]) 它可以正常工作并返回具有以下结构的 object:
listing_id: 0,
listing_rating: 1.5,
listing_reviews: [] <---Full of objects
As soon I try to access one of those keys it fails by giving me an undefined error.一旦我尝试访问其中一个键,它就会通过给我一个未定义的错误来失败。 Please let me know if you would like some more info as well.
如果您还需要更多信息,请告诉我。 Thank you.
谢谢你。
The problem is that you're rendering the result of console.log
in your Reviews.jsx file.问题是您在 Reviews.jsx 文件中呈现
console.log
的结果。 Anything you put in curly braces {}
in JSX, React will render.你在 JSX 中放在大括号
{}
中的任何内容,React 都会渲染。 So if you call a function, React will try to render the return value of that function. React throws an error if you try to render undefined
.因此,如果您调用 function,React 将尝试呈现该 function 的返回值。如果您尝试呈现
undefined
,React 会抛出错误。 And since console.log
always returns undefined
, you're always going to get an error.由于
console.log
总是返回undefined
,你总是会得到一个错误。 Even if you eventually have data in your listingData
prop, since you're still trying to render the return of console.log
and it still returns undefined
, you'll get an error.即使您最终在
listingData
道具中有数据,因为您仍在尝试呈现console.log
的返回并且它仍然返回undefined
,您将收到错误。
So, just get rid of the console.log
.所以,只需摆脱
console.log
。 Also, you will probably want to check that you actually have data in your listingData
prop.此外,您可能想要检查您的
listingData
道具中是否确实有数据。 Otherwise you'll get an error before your data actually loads.否则,您会在数据实际加载之前收到错误消息。
So, something like this for your Reviews.jsx
file:所以,你的
Reviews.jsx
文件是这样的:
import React, { useState } from 'react';
const Reviews = ({ listingData }) => {
// you can still add a debug line up here. that won't be an issue
console.log(listingData);
const reviews = listingData.length ? listingData[0].listing_reviews : null;
return (
<div>
{reviews}
</div>
)
}
export default Reviews;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.