react学习中遇到的错误及解决方法

错误一:index.js:2178 Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.

     原因是,代码中引用的react-bootstrap中的错误:

将这段代码:

<NavItem eventKey={1} >
    <Link to="/posts">posts</Link>

</NavItem>

改为:

<NavItem eventKey={1} href="/posts">
   posts
</NavItem>

错误二:index.js:2178 Warning: Each child in an array or iterator should have a unique "key" prop.

     原因为在引用map的时候,应该要给每一个子标签添加特定的key。

    将代码:

{posts.map((post)=>(
        <div >
            <h3>{post.title}</h3>
            <h4>{post.author}</h4>

<p className="content">{post.body}</p>

</div>

))}

//改为:

{posts.map((post)=>(
        <div key={post.id}>
            <h3>{post.title}</h3>
            <h4>{post.author}</h4>

<p className="content">{post.body}</p>

</div>

))}




猜你喜欢

转载自blog.csdn.net/zj1422424830/article/details/79480361