简体   繁体   English

为什么说嵌套块是冗余的非独行块?

[英]why it's saying Nested block is redundant no-lone-block?

`
import React from 'react';
import BuildControl from './BuildControl/BuildControl';
import './BuildControl.css';
const controls = [{ label: 'Cheese', type: 'cheese' }, { label: 'Bacon', type: 'bacon' }, { label: 'Meat', type: 'meat' }, { label: 'Salad', type: 'salad' }];


const BuildControls = () => {
    controls.map((ctrl) => {
        console.log("ctrl is:", ctrl.label, ctrl.type);
        return (<div className="BuildControls"><BuildControl key={ctrl.type} label={ctrl.label}></BuildControl></div>);
    });

}
export default BuildControls;

` enter image description here Eslint is not allowing to return from the callback or something else is wrong. ` 在此处输入图像描述 Eslint不允许从回调中返回或其他错误。

You did not type your code correctly in your question. 您在问题中输入的代码不正确。 You left out the block that eslint is complaining about. 您忽略了eslint抱怨的障碍。 The problem is the opening and closing brackets you have on lines by themselves is surrounding code that does not contain a function expression, or a const/let declaration which means there is no actual block being created. 问题是您在线上的左括号和左括号本身是围绕着不包含函数表达式或const / let声明的代码,这意味着没有实际的块被创建。

const BuildControls = () => {
    { // Beginning of lone block
        controls.map((ctrl) => {
            console.log("ctrl is:", ctrl.label, ctrl.type);
            return (<div className="BuildControls"><BuildControl key={ctrl.type} label={ctrl.label}></BuildControl></div>);
        });

    } // End of lone block
}
export default BuildControls;

See https://eslint.org/docs/rules/no-lone-blocks 参见https://eslint.org/docs/rules/no-lone-blocks

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

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