[英]Objects are not valid as a React child: Trying to show my posts
我的 React 和 Python 項目有問題,我正在從我的 API 中獲取帖子並嘗試在屏幕上顯示它們,但我收到此錯誤:
錯誤:對象作為 React 子對象無效(找到:object 和鍵 {Post})。 如果您打算渲染一組子項,請改用數組。
我正在使用額外的 package 來幫助我進行無限滾動,但它不起作用。 我認為問題在於使用concat
方法。
當我向我的項目添加POST
請求時,此問題開始出現,但這是一個GET
請求,所以..
const Crypto = (props) => {
const [posts, setPosts] = useState([]);
const [skip, setSkip] = useState(0);
const [hasMore, setHasMore] = useState(true);
useEffect(() => {
loadFunc();
}, []);
const loadFunc = () => {
axios
.get(API_CRYPTO + skip)
.then((res) => {
if (res.data.messages.length === 0) {
setHasMore(false);
return;
}
setPosts(posts.concat(res.data.messages)); // here is the problem!
setSkip(skip + 20);
})
.catch((err) => {
// handle error
console.error(err);
});
};
return (
<InfiniteScroll
dataLength={posts.length} //This is important field to render the next data
next={loadFunc}
hasMore={hasMore}
loader={
<div className="spinner">
<PuffLoader color={"#ffcb6b"} size={50} />
</div>
}
scrollableTarget="scrollableDiv"
>
{posts.map((post) => (
<Post
channel_id={post.channel_id}
channel_pp={post.channel_image}
message={post.message_text}
channelName={post.channel_nickname}
timeStamp={post.message_ts}
moonCounter={post.likes}
messageImage={(post.photo_params || 0 || {}).photo_url}
postID={post._id.$oid}
key={post._id.$oid}
/>
))}
</InfiniteScroll>
);
};
這是我的Post
組件返回的內容:
<Card id="card">
<img src={Dots} alt="dots" className="dots"/>
<NavLink to={"/" + props.channel_id} style={{ textDecoration: 'none'}} className="pp-link">
<img src={props.channel_pp} alt="pp" className="pp" />
<p className="pp-user-name" to={"/" + props.channel_id}>{props.channelName}</p>
</NavLink>
<p className="time-elapsed">{postedAgo}</p>
{props.messageImage ? <img className="message-img" alt="message-img" src={props.messageImage} onClick={ModalHandler}/> : null}
<Linkify componentDecorator={componentDecorator}>
<p className="post-body">
{props.message}
</p>
</Linkify>
<div className="interact">
<Comment/>
<Moon moonCounter={props.moonCounter} postID={props.postID} />
</div>
</Card>
問題已解決!
這是服務器錯誤,我的新帖子與其他帖子的 object 值不同,這是一個后端錯誤,非常感謝您的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.