簡體   English   中英

組件未在反應 js 中呈現

[英]Component is not rendering in react js

為什么這個組件沒有在 react js 中呈現。它顯示錯誤消息Unhandled Runtime Error Error: Element type is invalid: expected a string (for built-in components) or a class/function (for Composite components) 但得到:object。 您可能忘記從定義組件的文件中導出組件,或者您可能混淆了 default 和命名 imports

這是我的組件

function Test({session}) {
    const RenderPage=fetchComment &&(
                fetchComment?.map((comment)=>{
                    return <LoadComment
                    key={i}
                    timestamp={comment.timestamp?.toDate()}
                    comment={comment.comment}
                    image={comment.image}
                    user={comment.user} />
                })
                )
return (
<RenderPage></RenderPage>
)
}
return (
{RenderPage}
)

更改返回語句。 因為它是一個變量而不是一個組件。

RenderPage不是一個有效的 React 組件,如果fetchComment是 undefined/falsey,它要么是假的,要么是一個 JSX 數組。 只需返回計算的RenderPage值。 我還建議取消 PascalCasing 變量以減輕任何未來的閱讀混亂,轉換回 camelCase。 為了涵蓋您不返回數組的情況,有條件地返回null以獲得來自Test組件的有效 JSX 返回。

function Test({session}) {
  const renderPage = fetchComment ? fetchComment.map((comment, i) => {
    return (
      <LoadComment
        key={i}
        timestamp={comment.timestamp?.toDate()}
        comment={comment.comment}
        image={comment.image}
        user={comment.user}
      />
    );
  }) : null;
  return renderPage;
}

利用

export default Test({session}) {
const RenderPage=fetchComment &&(
            fetchComment?.map((comment)=>{
                return <LoadComment
                key={i}
                timestamp={comment.timestamp?.toDate()}
                comment={comment.comment}
                image={comment.image}
                user={comment.user} />
            })
            )
return (
   {RenderPage}
 )

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM