javascript - “必须返回一个有效的 React 元素(或 null)。” 与 array.map
问题描述
我有下面定义的功能性反应组件:
import React from 'react';
import Cell from './FeedCell';
import PropTypes from 'prop-types';
const Feed = (props) => {
return props.arr.map((type, index) => {
return (<Cell />)
})
};
我收到错误消息:
Feed(...):必须返回一个有效的 React 元素(或 null)。您可能返回了未定义、数组或其他一些无效对象。
我觉得这应该是显而易见的,但我已经尝试了在线提供的解决方案,但我遗漏了一些东西。
解决方案
您当前正在返回一个数组,但您必须返回一个正确的 React 元素。
React.Fragment
如果您不想在 DOM 中包含包装元素,则可以将数组包装在 a中。
const Feed = (props) => {
return (
<React.Fragment>
{props.arr.map((type, index) => <Cell />)}
</React.Fragment>
);
};
推荐阅读
- swift - 类函数可以返回类本身吗?
- ios - 执行过渡动画时,视图将移出父边界
- c++builder - 如何在 C++Builder 中选择停靠的 VCL 表单
- android - 访问片段内部的数据库处理程序
- php - 为什么 JSON 只为我的查询返回 1 个图像输出?
- git - 当 Git 创建标签时(`git tag -a` 命令没有指定提交) - 它是否从 HEAD 获得参考?
- python - 如何从另一个列字符串值中删除列字符串值?
- c - Swift:在导入的 c 库中找不到函数
- .htaccess - 如何传递 id 但不在 URL 中显示
- c++ - 如何在 Windows 上将犰狳与 nvblas 链接