1 回答

TA貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊
console.log問題是您正在Reviews.jsx 文件中呈現(xiàn)結(jié)果。在 JSX 中放入大括號中的任何內(nèi)容{},React 都會渲染。因此,如果你調(diào)用一個函數(shù),React 將嘗試渲染該函數(shù)的返回值。如果您嘗試渲染,React 會拋出錯誤undefined。由于console.log總是返回undefined,所以你總是會得到一個錯誤。即使您最終在listingDataprop 中有數(shù)據(jù),由于您仍在嘗試渲染返回值console.log并且它仍然返回undefined,因此您會收到錯誤。
所以,只要擺脫console.log. 另外,您可能需要檢查道具中是否確實(shí)有數(shù)據(jù)listingData。否則,在數(shù)據(jù)實(shí)際加載之前您會收到錯誤消息。
所以,你的文件是這樣的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;
添加回答
舉報