简体   繁体   English

为什么我的 React 应用程序总是无法编译?

[英]Why Does My React App Keep Failing To Compile?

I'm learning react and building a simple straightforward to do list app.我正在学习 React 并构建一个简单明了的待办事项列表应用程序。 However, it keeps throwing this error:但是,它不断抛出此错误:

./src/App.js
  Line 9:35:  'Todo' is not defined  react/jsx-no-undef

Search for the keywords to learn more about each error.

I'm not sure why this error keeps throwing.我不确定为什么这个错误不断抛出。 I have defined my my ToDo js file - I believe I correctly exported it and then correctly imported in the App js file.我已经定义了我的 ToDo js 文件 - 我相信我正确导出了它,然后在 App js 文件中正确导入。 Am I missing something?我错过了什么吗?

App.Js File: App.Js 文件:

import React from 'react';
import logo from './logo.svg';
import './App.css';
import ToDo from './ToDo.js'
import DoData from './DoData.js'

function App() {

  const val = DoData.map(item => <Todo key = {item.id} item = {item}/>)


  return (
   {val}
  );
}

export default App;

ToDo.js file: ToDo.js 文件:

import React from 'react'

function Todo(props){
    return(
        <div className = "todo-item">
            <input type ="checkbox" checked = {props.things.completed}></input>
    <p>{props.things.description}</p>
        </div>
    )
}

export default Todo

DoData.js: DoData.js:

let DoData = [
    {
        id: 1,
        description: "go for a walk",
        completed: true
    },
    {
        id: 2,
        description: "babysitting",
        completed: true 
    },
    {
        id: 3,
        description: "watch MadMen",
        completed: false 
    },
    {
        id: 4,
        description: "See the mailman",
        completed: true 
    },
    {
        id: 5,
        description: "Say hello to the world",
        completed: false 
    }
]

export default DoData.js

In JavaScript, identifiers are case-sensitive在 JavaScript 中,标识符区分大小写

Check the case of the identifier in your import statement in the file ./App.Js vs the use of it:检查文件./App.Js 导入语句中标识符的./App.Js与使用情况:

import ToDo from './ToDo.js'
<Todo ... />

Notice the difference in casing between Todo & ToDo .注意TodoToDo之间大小写的区别。 You have 2 options to fix this linting error.您有 2 个选项可以修复此 linting 错误。

Either change your import to:要么将您的导入更改为:

import Todo from './ToDo.js'

or或者

change your use to:将您的用途更改为:

`<ToDo ... />`

Note: Also, double check your export statement in the file ./DoData.js .注意:另外,仔细检查文件./DoData.js中的导出语句。
While the identifier DoData definitely exists, it has no property called js , and so DoData.js will be undefined.虽然标识符DoData肯定存在,但它没有名为js属性,因此DoData.js将是未定义的。

Option 1选项1

Update
import ToDo from './ToDo.js'
to
import Todo from './ToDo.js'

as you are using <Todo key = {item.id} item = {item}/> in your app

Option 2选项 2

Update
<Todo key = {item.id} item = {item}/>
to
<ToDo key = {item.id} item = {item}/>

as you are importing component as ToDo
import ToDo from './ToDo.js'

As you are exporting ToDo as default in ToDo.js file, you can use it as any name as you want in your imports anywhere in your app.当您在 ToDo.js 文件中默认导出 ToDo 时,您可以根据需要在应用程序的任何位置的导入中使用它作为任何名称。 But make sure when you render the component, it matches the import name at top.但是请确保在渲染组件时,它与顶部的导入名称匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM