为什么在运行react js组件类时出现“ no-undef”错误?

拉加温德(Ragawender Sivanraj)

我是新来反应js的人。我创建了一个扩展Component的类。当我运行我的代码时,它会引发错误,即未定义变量。我正在学习一个教程。这对于导师来说是正确的。但是我出错了。

App.js

import logo from './logo.svg';
import './App.css';
import React from 'react';
import TextCard from './textCard';


class App extends React.Component {

 
  
  textArr = [
    {
      id: 1,
      name:"Name1",
      department:"Computer",
      semester :7,
    },
    {
      id:2,
      name: "Name2",
      department: "Computer",
      semester: 7,
    },
    {
      id: 3,
      name: "Name3",
      department: "Computer",
      semester: 7,
    }
  ]

  

  textCards = this.textArr.map((item)=>{
    return (
      <TextCard  key={item.id} name={item.name} department={item.department} semester={item.semester} />
     )
  })

  hideOnClick(){
    alert('Hide btn pressed')
  }

  text1 = "Testing variable access"

  render(){
    return(
      <div className="App">
        <h1>My React js</h1>
        <h2>React js course from Udemy</h2>
        <hr></hr>
        <h3>JSX test</h3>
        <hr></hr>
        <div>{this.text1}</div>
        <hr></hr>
        <button onClick={this.hideOnClick}>Hide List</button>
        <div>
          {this.textCards}
        </div>
    </div>
    );
  }
 
}

export default App;

textCard.js

import React from 'react';
import classess from './textCard.module.css';

const TextCard = (props) =>{
    return(
        <div className={classess.dynamicTest}>
            <h2>{props.name}</h2>
            <p>{props.department}</p>
            <p>{props.semester}</p>
        </div>
    )
}

export default TextCard;

错误

编译失败。

src \ App.js第11:3行:未定义'textArr'no-undef
第34:3行:未定义'textCards'no-undef
第50:3行:未定义'text1'no -undef

搜索关键字以了解有关每个错误的更多信息。

安迪·雷(Andy Ray)

您需要安装并使用@ babel / plugin-proposal-class-properties来使用类字段类字段还不是Javascript核心语言,因此如果您想使用它们,则需要告诉Babel编译器如何处理它们。

npm install --save-dev @babel/plugin-proposal-class-properties

并将其添加到您的 .babelrc.json

 "plugins": ["@babel/plugin-proposal-class-properties"]

如果您的教程没有提到您需要安装此插件,那么它不是一个很好的教程。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在运行“ bower install”时出现此错误?

为什么在运行optaplanner .jar时出现此错误?

为什么默认的导出组件在React JS中给出no-undef错误?

为什么该程序在运行时出现错误

为什么我在运行代码时遇到错误?

为什么在运行简单的docker映像时出现oci运行时错误?

React JS错误:未定义react / jsx-no-undef

当“堆栈测试”工作时,为什么在运行“堆栈haddock”时出现构建计划错误?

React Js:未定义mountNode no-undef错误

为什么在运行基本的Django查询时出现int()异常错误的无效文字?

为什么在运行exe文件时cx_Freeze出现此错误?

为什么在运行代码时出现调试断言失败错误

为什么在运行项目时出现此谷歌番石榴错误?

为什么在运行git filter-branch时出现pathspec错误?

为什么在运行sudo`apt-get update`时出现错误?

为什么在运行jupyter笔记本时出现错误?

为什么在运行Linux find命令时出现“权限被拒绝”错误消息

为什么在运行动态 Web 项目时出现 HTTP 404 错误?

什么是退出值5,为什么在运行C程序时仍然出现此错误?

为什么在运行时出现Typescript编译错误?

为什么在运行 sudo docker-compose up 时会出现权限错误?

为什么在运行 npx run dev 时会出现找不到模块错误?

为什么在运行时出现java.lang.StringIndexOutOfBoundsException错误?

为什么在运行下面的代码时会出现stackoverflow错误?

为什么在运行Spring Boot Project时总是出现状态为“ 404”的Whitelabel错误页面

为什么在运行GET请求onChange时仅遇到CORS错误?

为什么在运行Webkit时pow()计算错误?

为什么在运行Protractor测试时弹出Firefox错误消息?

为什么我在运行 npm install 时在 bcrypt 中收到错误 404?