簡體   English   中英

得到預期的賦值或函數調用的錯誤,而是在反應中看到了一個表達式 no-unused-expressions

[英]getting an error of Expected an assignment or function call and instead saw an expression no-unused-expressions in react

我在下面收到錯誤

第 56:11 行:期望賦值或函數調用,但看到的是表達式 no-unused-expressions

<div className="posts_container">
{
    (userPosts.length) ?
        (
            <div>
            {
                userPosts.map((post,idx)=>{
                    <div className="smallPost">
                        <img className='smallPost_pic' alt='post_img' src={post.imageurl}/>
                    </div>
                })
            }
            </div>
        )
        :
        (
            <h1> No posts Yet </h1>
        )
}
</div>

請幫我解決這個問題。 提前致謝。

傳遞給.map的函數沒有返回任何內容。

所以要么添加return

userPosts.map((post,idx) => {
  return (
    <div className="smallPost">
      <img className='smallPost_pic' alt='post_img' src={post.imageurl}/>
    </div>
  )
})

或用括號替換花括號:

userPosts.map((post,idx) => (
    <div className="smallPost">
      <img className='smallPost_pic' alt='post_img' src={post.imageurl}/>
    </div>
  )
)

作為旁注,請記住向從.map函數返回的div添加一個key 更多關於 React 的文檔: Lists and Keys

暫無
暫無

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

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