首页 > 解决方案 > 我在 react js 中遇到此错误 -- src\components\Allcourse.js Line 20:35: 'course' is not defined no-undef

问题描述

import React, { useState } from 'react';
import Course from './Course';

const Allcourse = () => {
  const [courses, setCourse] = useState([
    { title: 'java course', description: 'this is demo' },
    { title: 'react course', description: 'this is demo' },
    { title: 'django course', description: 'this is demo' },
  ]);
  return (
    <div>
      <h1>All course</h1>
      <p>list of courses</p>

      {courses.length > 0
        ? courses.map(item => <Course course={item} />)
        : 'No course'}
    </div>
  );
};
export default Allcourse;

标签: reactjs

解决方案


我试图复制您的代码以重现错误。从我看到的情况来看,您可能错误地导入了课程组件。仔细检查您的文件路径并确保命名全部正确。

这里有一个可供参考的代码框: https ://codesandbox.io/s/dark-flower-1p160?file=/src/App.js


推荐阅读