gpt4 book ai didi

javascript - "A valid React element (or null) must be returned."与 array.map

转载 作者:行者123 更新时间:2023-12-03 16:39:07 25 4
gpt4 key购买 nike

我在下面定义了 Functional React 组件:

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(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

我觉得这应该是显而易见的,但我已经尝试过在线提供的解决方案,但我遗漏了一些东西。

最佳答案

您当前正在返回一个数组,但您必须返回一个正确的 React 元素。

如果您不想在 DOM 中使用包装元素,您可以将数组包装在 React.Fragment 中。

const Feed = (props) => {
return (
<React.Fragment>
{props.arr.map((type, index) => <Cell />)}
</React.Fragment>
);
};

关于javascript - "A valid React element (or null) must be returned."与 array.map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51275495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com