简体   繁体   English

React无状态组件中'const'变量的顺序[重复]

[英]Order of 'const' variables in React Stateless Components [duplicate]

This question already has an answer here:这个问题在这里已经有了答案:

Say I have a simple React stateless component like so:假设我有一个简单的 React 无状态组件,如下所示:

const myComponent = () => {
    const doStuff = () => {
        let number = 4;

        return doubleNumber(number);
    };

    const doubleNumber = number => {
        return number * 2;
    };

    return <div>Hello {doStuff()}</div>;
};

export default myComponent;

Based on the eslint error I receive, and my understanding of how 'const' works, I assumed that this component would not render, since the function 'doubleNumber()' is used by the function 'doStuff()' before it is initialized.根据我收到的 eslint 错误,以及我对“const”如何工作的理解,我假设该组件不会呈现,因为 function“doStuff()”在初始化之前使用了 function“doubleNumber()”。 However, whenever I use this component, it renders as expected - why doesn't it throw an exception?但是,每当我使用这个组件时,它都会按预期呈现 - 为什么它不抛出异常? Does this mean the order of 'const' variables in React components can be whatever we like?这是否意味着 React 组件中 'const' 变量的顺序可以是我们喜欢的任何顺序?

The reason the code works is because the body of doStuff isn't executed until it's invoked.代码有效的原因是doStuff的主体在被调用之前不会执行。 Since doubleNumber happened to be defined before doStuff was called, everything is fine, but the code is correctly identified as brittle by the linter because the dependency inversion.由于doubleNumber恰好在调用doStuff之前定义,所以一切都很好,但是由于依赖倒置,代码被 linter 正确识别为易碎代码。

The crash occurs only if you happened not to initialize doubleNumber by the time doStuff was called:只有在调用doStuff时碰巧没有初始化doubleNumber时才会发生崩溃:

const doStuff = () => doubleNumber(4);
doStuff(); // ReferenceError: doubleNumber is not defined
const doubleNumber = number => number * 2;

which seems obvious here, but in more complex cases may not be so clear.这在这里似乎很明显,但在更复杂的情况下可能不是那么清楚。

const versus let should have no bearing on the linter's output because although they're hoisted, they cannot be accessed until initialized. constlet应该与 linter 的 output 无关,因为尽管它们被提升,但在初始化之前无法访问它们。

The order can be whatever you like, assuming calls are only made once dependencies are available, but that doesn't make it a good idea (which is exactly what linting is supposed to identify).顺序可以是您喜欢的任何顺序,假设仅在依赖项可用时才进行调用,但这并不是一个好主意(这正是 linting 应该识别的内容)。

Your case is described in the Typescript documentation of Block scoped variables (navigate to the last bit of that section).您的案例在块范围变量的 Typescript 文档中进行了描述(导航到该部分的最后一位)。

It says:它说:

you can still capture a block-scoped variable before it's declared.您仍然可以在声明之前捕获块范围的变量。 The only catch is that it's illegal to call that function before the declaration.唯一的问题是在声明之前调用 function 是非法的。 If targeting ES2015, a modern runtime will throw an error;如果以 ES2015 为目标,现代运行时将抛出错误; however, right now TypeScript is permissive and won't report this as an error.但是,现在 TypeScript 是允许的,不会将其报告为错误。

And the example given is给出的例子是

function foo() {
    // okay to capture 'a'
    return a;
}

// illegal call 'foo' before 'a' is declared
// runtimes should throw an error here
foo();

let a;

In your case, doubleNumber is being captured inside doStuff , but doubleNumber is declared before doStuff and hence is okay according to the docs, just like the variable a in the documentation's example.在您的情况下, doubleNumberdoStuff中被捕获,但是doubleNumberdoStuff之前声明,因此根据文档是可以的,就像文档示例中的变量a一样。

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

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