reactjs - "famous"元素类型无效错误

标签 reactjs

今天早上我尝试将所有组件放入 CSS 网格中。但我遇到了一个错误。

我已经确定了导致问​​题的组件,但即使在尝试了 Stack Overflow 上建立的所有解决方案(它提供了

错误在这里

*Element type is invalid:
expected a string (for built-in components)
or a class/function (for composite components)
but got: undefined. 
You likely forgot to export your component from the file it's defined in,
or you might have mixed up default and named imports.

这里是(顽皮的)组件

import React  from "react";
import {ResponsiveContainer,AreaChart,Area,CartesianGrid,XAxis,YAxis,Tooltip,Legend} from "recharts";

const ErrorChart = ({data}) =>{


return (
    <div  className="chart-container" style={{ width: '40%', height: 300 }}>
    <ResponsiveContainer>
      <AreaChart
        data={data}
        margin={{
          top: 10, right: 30, left: 0, bottom: 0,
        }}
      >
        <CartesianGrid strokeDasharray="3 3" />
        <XAxis dataKey="time" />
        <YAxis />
        <Tooltip />
        <Area type="monotone" dataKey="number" stroke="#8884d8" fill="#8884d8" />
      </AreaChart>
      <Legend/>
    </ResponsiveContainer>
  </div>
  );

}
export default ErrorChart

最佳答案

经过试验,要消除错误,似乎Legend必须位于ResponsiveContainer之外:

<div className="chart-container" style={{ width: "40%", height: 300 }}>
      <ResponsiveContainer>
        <AreaChart
          data={data}
          margin={{
            top: 10,
            right: 30,
            left: 0,
            bottom: 0
          }}
        >
          <CartesianGrid strokeDasharray="3 3" />
          <XAxis dataKey="time" />
          <YAxis />
          <Tooltip />
          <Area
            type="monotone"
            dataKey="number"
            stroke="#8884d8"
            fill="#8884d8"
          />
        </AreaChart>
      </ResponsiveContainer>
      <Legend />
    </div>

working example

关于reactjs - "famous"元素类型无效错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58112645/

相关文章:

google-maps - 使用 React 实现谷歌地图

javascript - null 不是对象(评估 '_RNGestureHandlerModule.default.Direction' )

javascript - 在传递 props 之前 react 变异状态

javascript - 客户端 react 脚本覆盖服务器端渲染的 Prop

javascript - 如果 CSS 省略号在 React 组件中处于事件状态,我如何有条件地显示标题工具提示

reactjs - React fullcalendar错误无法读取未定义的属性 'seg'

reactjs - 测试 Material UI Radio Checked Value

reactjs - 类型错误 : Class extends value undefined is not a constructor or null while running next. js

javascript - 使用 <Provider> 通过上下文传递存储在基于 connect() 的场景中不起作用

javascript - 在多维数组 Javascript 中使用映射