【问题标题】:TypeError: .map is not a function don't understand what's wrongTypeError: .map is not a function 不明白出了什么问题
【发布时间】:2021-12-15 04:19:04
【问题描述】:

我不明白为什么这个组件不起作用 这是指向 img 的链接“因为不知何故我无法直接发布图片 [1]:https://i.stack.imgur.com/Ax7im.png 感谢您的帮助 这里有代码:

import React from 'react';
import ReactDOM from 'react-dom';
import Postitem from "./Postitem";

const Postlist=(post,title)=>{

return (
<div>
         <h1 style={{textAlign: 'center'}}>{title}</h1>
{post.map((post) =>
 <Postitem post={post} key={post.id}/>)};
</div>
 
)};
export default Postlist;
      

【问题讨论】:

  • 发布一个数组吗?

标签: javascript reactjs web components typeerror


【解决方案1】:

React 函数组件只接受一个参数,它是一个包含所有道具的对象。假设您在渲染时将 post 属性作为数组传递,将函数声明修复为此将起作用:

const Postlist=({post,title})=>{
  // component body here, as above
}

【讨论】:

    【解决方案2】:

    检查,该组件的“post”变量中的内容。如果你使用“post.map”函数,那么它必须是一个数组。

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多