简体   繁体   English

编译失败:<function name> 未定义 no-undef</function>

[英]Failed to compile: <function name> is not defined no-undef

I have a simple functional component in reactJS with arrow function defined inside of it.我在 reactJS 中有一个简单的功能组件,其中定义了箭头 function。 For some reason it throws a "not defined" error through the function exits.由于某种原因,它通过 function 退出抛出“未定义”错误。 It works in my other repository not sure why it started to throw this error.它在我的其他存储库中工作,不确定它为什么开始抛出此错误。

My code:我的代码:

Template.js模板.js

// Dependencies
import React, { useState } from 'react';


function Login(props) { 


    handleChange = () => {
        console.log('Hello');
    }


    return (
        <div>
            <p> Hello </p>
        </div>
    );
};


export default Login;

Error:错误:

./src/screens/Template.js Line 8: 'handleChange' is not defined no-undef ./src/screens/Template.js 第 8 行:'handleChange' 未定义 no-undef

Search for the keywords to learn more about each error.搜索关键字以了解有关每个错误的更多信息。 This error occurred during the build time and cannot be dismissed.此错误发生在构建期间,无法消除。

You have to write "const" at your arrow function's beginning.您必须在箭头函数的开头写上“const”。 Because it's not in a class component.因为它不在 class 组件中。

const handleChange = () => { console.log('Hello'); }

or you can use just with;或者你可以只用;

function handleChange() { console.log('Hello'); }

handleChange is a variable you defined, so please try to add const or let before handleChange to see if the error dismissed. handleChange是您定义的变量,因此请尝试在handleChange之前添加constlet以查看错误是否消除。

// Dependencies
import React, { useState } from 'react';


function Login(props) { 


    const handleChange = () => {
        console.log('Hello');
    }


    return (
        <div>
            <p> Hello </p>
        </div>
    );
};


export default Login;

use const as to define a function.使用const定义一个 function。 But I suggest you to write like this:但我建议你这样写:

// Dependencies
import React, { useState } from 'react';

// Outside of your component, for performance 
const handleChange = () => {
    console.log('Hello');
}

function Login(props) { 
  return (
    <div>
        <p> Hello </p>
    </div>
  );
};


export default Login;

The reason for the error is that the variable handleChange has not been declared.错误的原因是变量handleChange没有被声明。 In Javascript you can declare it in one of these ways:在 Javascript 中,您可以通过以下方式之一声明它:

  • var handleChange = () => {...} var handleChange = () => {...}
  • const handleChange = () => {...} const handleChange = () => {...}
  • let handleChange = () => {...}让 handleChange = () => {...}
  • function handleChange() {...} function handleChange() {...}

But you cannot simply start using handleChange without declaring it as a variable.但是您不能简单地开始使用handleChange而不将其声明为变量。 The language parser doesn't know what it is and gives the error you see.语言解析器不知道它是什么并给出您看到的错误。

If it was working in similar code, then the variable must have been declared earlier in the program, but I'd need to see the other implementation to know for sure.如果它在类似的代码中工作,那么该变量必须在程序的早期声明,但我需要查看其他实现才能确定。 Perhaps it was like this:也许是这样的:

var handleChange;

/* some other code here */
handleChange = () => { console.log("I work because I was declared earlier"); }

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

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