正文从这开始~

总览

当我们对一个不是数组的值调用map()方法时,就会产生"TypeError: map is not a function"错误。为了解决该错误,请将你调用map()方法的值记录在console.log上,并确保只对有效的数组调用map

这里有个示例来展示错误是如何发生的。

const App = () => {
const obj = {}; // ️ Uncaught TypeError: map is not a function return (
<div>
{obj.map(element => {
return <h2>{element}</h2>;
})}
</div>
);
}; export default App;

我们在一个对象上调用Array.map()方法,得到了错误反馈。

为了解决该错误,请console.log你调用map方法的值,确保它是一个有效的数组。

export default function App() {
const arr = ['one', 'two', 'three']; return (
<div>
{arr.map((element, index) => {
return (
<div key={index}>
<h2>{element}</h2>
</div>
);
})}
</div>
);
}

Array.isArray

你可以通过使用Array.isArray方法,来有条件地检查值是否为数组。

const App = () => {
const obj = {}; return (
<div>
{Array.isArray(obj)
? obj.map(element => {
return <h2>{element}</h2>;
})
: null}
</div>
);
}; export default App;

如果值为数组,则返回对其调用map方法的结果,否则返回null。这种方式不会得到错误,即使值不是一个数组。

如果值是从远程服务中获取,请确保它是你期望的类型,将其记录到控制台,并确保你在调用map方法之前将其解析为一个原生JavaScript数组。

Array.from

如果有一个类数组对象,在调用map方法之前你尝试转换为数组,可以使用Array.from()方法。

const App = () => {
const set = new Set(['one', 'two', 'three']); return (
<div>
{Array.from(set).map(element => {
return (
<div key={element}>
<h2>{element}</h2>
</div>
);
})}
</div>
);
}; export default App;

在调用map方法之前,我们将值转换为数组。这也适用于类数组的对象,比如调用getElementsByClassName方法返回的NodeList

Object.keys

如果你尝试迭代遍历对象,使用Object.keys()方法获取对象的键组成的数组,在该数组上可以调用map()方法。

export default function App() {
const employee = {
id: 1,
name: 'Alice',
salary: 100,
}; return (
<div>
{/* ️ iterate object KEYS */}
{Object.keys(employee).map((key) => {
return (
<div key={key}>
<h2>
{key}: {employee[key]}
</h2> <hr />
</div>
);
})} <br />
<br />
<br /> {/* ️ iterate object VALUES */}
{Object.values(employee).map((value, index) => {
return (
<div key={index}>
<h2>{value}</h2> <hr />
</div>
);
})}
</div>
);
}

我们使用Object.keys方法得到对象的键组成的数组。

const employee = {
id: 1,
name: 'Alice',
salary: 100,
}; // ️ ['id', 'name', 'salary']
console.log(Object.keys(employee)); // ️ [1, 'Alice', 100]
console.log(Object.values(employee));

我们只能在数组上调用map()方法,所以我们需要获得一个对象的键或者对象的值的数组。

React报错之map() is not a function的更多相关文章

  1. react报错this.setState is not a function

    当报错这个的时候就要看函数是否在行内绑定this,或者在constructor中绑定this. 我这里犯的错误的是虽然我在constructor中绑定了this,但是语法写的不正确. 错误示范: co ...

  2. Vue.js搭建路由报错 router.map is not a function,Cannot read property ‘component’ of undefined

    错误: 解决办法: 2.0已经没有map了,使用npm install vue-router@0.7.13 命令兼容1.0版本vue 但是安装完之后会出现一个错误: Cannot read prope ...

  3. React报错之Expected an assignment or function call and instead saw an expression

    正文从这开始~ 总览 当我们忘记从函数中返回值时,会产生"Expected an assignment or function call and instead saw an express ...

  4. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  5. myeclipse 10 载入新的项目报错Cannot return from outside a function or method

    myeclipse 10 载入新的项目报错Cannot return from outside a function or method 解决方法: 方法一: window -->prefere ...

  6. SAP MM ME29N 试图取消审批报错 - Document has already been outputed(function not possible) -

    SAP MM ME29N 试图取消审批报错 - Document has already been outputed(function not possible) - 今天收到用户的一个问题,说他试图 ...

  7. React报错 :browserHistory doesn't exist in react-router

    由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...

  8. 【已解决】React项目中按需引入ant-design报错TypeError: injectBabelPlugin is not a function

    react项目中ant-design按需加载,使用react-app-rewired的时候报错 运行npm start或者yarn start报如下错误: TypeError: injectBabel ...

  9. react报错 TypeError: Cannot read property 'setState' of undefined

    代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...

随机推荐

  1. Docker学习重点(7)~DockerFile

    一.DockerFile DockerFile是用来构建docker镜像的文件,可以理解为命令参数脚本! 1.构建步骤: 编写一个dockerfile文件 docker build 构建成为一个镜像 ...

  2. (原创)[C#] MEF 主程序与插件加载不同版本的DLL

    一.前言 MEF(Managed Extensibility Framework),是轻量级的插件框架.使用简单,功能强大.详细介绍见MSDN,本文不再赘述. 在使用MEF时,会遇到这样一种场景: 主 ...

  3. 如何生成一个java文档

    如何生成一个java文档 众所周知,一个程序给别人看可能可以看懂,几万行程序就不一定了.在更多的时候,我们并不需要让别人知道我们的程序是怎么写的,只需要告诉他们怎么用的.那么,api文档就发挥了它的作 ...

  4. 【多线程】线程强制执行 join()

    线程强制执行 join() Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞 : 可以想象成插队. 代码示例: /** * @Description 测试join方法 * @Auth ...

  5. Hadoop安装学习(第四天)

    学习任务:解决9000端口丢失导致hadoop无法连接的问题 解决方法:格式化namenode 步骤: 1.进入hadoop/bin 2.输入命令:hadoop namenode -format(hd ...

  6. 通过CSS让图片变的清楚

    image { width: 100%; height: 100%; border-radius: 10upx; //让图片变清楚 image-rendering: -moz-crisp-edges; ...

  7. Linux namespace技术应用实践--调用宿主机命令(tcpdump/ip/ps/top)检查docker容器网络、进程状态

    背景 最近偶然听了几堂极客时间的云原生免费公开课程,首次接触到了Linux namespace技术,并了解到这正是现在风头正劲的容器技术基石,引起了自己探究一二的兴趣,结合课程+网络搜索+实践操作,也 ...

  8. 【Java面试】请说一下Mysql索引的优点和缺点?

    今天分享的这道面试题,让一个工作4年的小伙子去大众点评拿了60W年薪. 这道面试题是: "请你说一下Mysql索引的优点和缺点" 关于这道题,看看普通人和高手的回答 普通人: 嗯. ...

  9. 开发工具-Redis Desktop Manager下载地址

    更新记录 2022年6月10日 完善标题. 官方: https://github.com/uglide/RedisDesktopManager 免费打包版: https://github.com/le ...

  10. SpringBoot之:SpringBoot的HATEOAS基础

    目录 简介 链接Links URI templates Link relations Representation models 总结 简介 SpringBoot提供了HATEOAS的便捷使用方式,前 ...