正文从这开始~

总览

当我们对一个不是数组的值调用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. Install Ubuntu on Windows Subsystem for Linux

    安装参考 ubuntu.com/wsl microsoft/wsl/install-manual microsoft/terminal 错误解决方案 github/启动 WSL 2时警告"参 ...

  2. 吊炸天,Spring Security还有这种用法!

    在用Spring Security项目开发中,有时候需要放通某一个接口时,我们需要在配置中把接口地址配置上,这样做有时候显得麻烦,而且不够优雅.我们能不能通过一个注解的方式,在需要放通的接口上加上该注 ...

  3. CentOS自动同步时间

    安装ntpdate yum install ntpdate -y 测试是否正常 ntpdate cn.ntp.org.cn # 正常情况 [root@centos7 www]# ntpdate cn. ...

  4. 每天一个 HTTP 状态码 101

    101 Switching Protocols 当客户端的请求具有 Upgrade HTTP 首部,表示要求服务器切换到指定协议:此时服务器端就可以向客户端响应 101 Switching Proto ...

  5. 双webview模式,子窗口打不开或者无法切换

    iOS 真机调试时,发现window.open 无效.可以结合plusReady里面不执行一起参考,博主在当时遇到这个问题只查询了资料,而后并没有来得及自己亲自验证以下方法的可行性.来日再遇上mui的 ...

  6. Django-request的常见属性

    瞧一瞧,看一看,Django时,获取Request的各个属性. Request的常见属性 request.META 返回一个python字典.它包含了所有的HTTP请求信息.如下代码: 点击查看代码 ...

  7. Python数据分析--Numpy常用函数介绍(9)--Numpy中几中常见的图形

    在NumPy中,所有的标准三角函数如sin.cos.tan等均有对应的通用函数. 一.利萨茹曲线 (Lissajous curve)利萨茹曲线是一种很有趣的使用三角函数的方式(示波器上显示出利萨茹曲线 ...

  8. php 访问控制可见性 public protected private

    对属性或方法的访问控制,是通过在前面添加关键字public(公有),protected(受保护的),private(私有)来实现. 被定义为公有的类成员可以在任何地方被访问. 被定义为受保护的类成员则 ...

  9. 聊聊 C# 和 C++ 中的 泛型模板 底层玩法

    最近在看 C++ 的方法和类模板,我就在想 C# 中也是有这个概念的,不过叫法不一样,人家叫模板,我们叫泛型,哈哈,有点意思,这一篇我们来聊聊它们底层是怎么玩的? 一:C++ 中的模板玩法 毕竟 C+ ...

  10. CentOS6安装使用ntp同步时间

    [root@server yum.repos.d]# yum install ntp已加载插件:fastestmirror, priorities, refresh-packagekit, secur ...