简体   繁体   English

在switch语句中使用break时出现无法访问的代码错误

[英]Unreachable code error when I use a break in my switch statement

I am wondering what I am doing wrong in my code. 我想知道我的代码中我做错了什么。 I am trying to do the following: 我正在尝试执行以下操作:

switch (action.type) {
    case TYPES.ADD_TO_FAVORITES:
      if (state.faves.length <= 10) {
        return assign({}, state, {
          faves: state.faves.concat(action.payload),
          full: false
        });
      } else {
        return assign({}, state, {
          faves: state.faves,
          full: true
        });
      }
    default:
      return state;
  }

My linter says to add a break before default case, but when I do that, it says unreachable code . 我的linter说要在default情况下添加一个中断,但是当我这样做时,它会说unreachable code

A return will act as a break . return将作为break There is no need to use both. 没有必要同时使用两者。 If you receive the message that you have unreachable code, then there is either no point in it being there, or something you did previously is out of order logically. 如果您收到的消息是您有无法访问的代码,那么它就没有任何意义,或者您之前执行的某些操作在逻辑上是无序的。

The linter rule ie 'no-fallthrough' in eslint, acts as to not allow any accidental fallthrough from case to case. 短绒规则,即在'eslint'中的'no-fallthrough',用于不允许任何意外的摔倒。

Meaning without break code execution will continue from matching case to next cases unless a break, return etc is encountered. 除非遇到中断,返回等,否则没有中断代码执行的含义将从匹配大小写继续到下一个大小写。

Sometimes we do need this but unintentional fallthrough can happen and this rule tries to prevent that. 有时候我们确实需要这个,但是可能会发生无意的崩溃,而这条规则试图阻止这种情况发生。

You can disable the rule or configure it as warning . 您可以禁用该规则或将其配置为警告。 I would recommend to have a variable assigned for return value and at the end of the function and return it without disabling the rule. 我建议为返回值指定一个变量,并在函数结束时返回它而不禁用该规则。

function() {
var returnvalue;
Switch(variableA) {
    Case 1:
        returnvalue = somevalue;
        break;
    case 2:
        returnvalue = some other value;
        break;
     default:
         returnvalue= default value;
    }
return returnvalue; 

} }

And for the unreachable part, you are returning from your if else block. 对于无法访问的部分,您将从if else块返回。

So the break will never get any chance to execute. 所以休息永远不会有任何机会执行。

switch (action.type) {
case TYPES.ADD_TO_FAVORITES:
  if (state.faves.length <= 10) {
    return ...;
  } else {
    return ...;
  }
  break; // YOU HAVE ADDED BREAK HERE
default:
  return state;
}

Inside case TYPES.ADD_TO_FAVORITES , either if or else will be executed. case TYPES.ADD_TO_FAVORITESifelse将被执行。 As in both if and else you have returned some object, the break you have added just before default , will never going to be executed! 因为在if和else你都返回了一些对象,你在default之前添加的break将永远不会被执行!

That's why it says it says unreachable code . 这就是它说它unreachable code

Return in switch statement make no sense and not allowed . 返回 switch语句 没有意义也不允许 You can only return in a function . 您只能在函数中 返回 If you want to get value back from switch statement. 如果你想从switch语句中获取值。 Just assign it to a variable like this 只需将它分配给这样的变量即可

var result;
switch (action.type) {
    case TYPES.ADD_TO_FAVORITES:
        if (state.faves.length <= 10) {
            result = assign({}, state, {
                faves: state.faves.concat(action.payload),
                full: false
            });
        } else {
            result = assign({}, state, {
                faves: state.faves,
                full: true
            });
        }
        break;
    default:
        result = state;
        break;
}

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

相关问题 检测到javascript开关中断无法访问的代码 - javascript switch break unreachable code detected 当我尝试在函数中添加 if 时,为什么会出现错误“无法访问的代码”? - Why i get error "unreachable code" when i try to put an if in my function? 通过数组映射的switch语句给出了无法到达的代码警告 - Switch statement that maps through array gives unreachable code warning Break对我的switch语句无效 - Break isn't working on my switch statement 当我尝试在我的代码中使用它来检查变量时,if 语句不起作用 - if statement not working when i try to use it in my code to check a variable 当我想在本地服务器上运行我的代码时,我总是会收到这种错误“SyntaxError: Cannot use import statement outside a module” - When I wants to run my code in local server i always get this kinds of error “SyntaxError: Cannot use import statement outside a module” 我可以使用 switch 语句来管理 React 中的表单吗? - Can I use a switch statement to manage my forms in React? 何时使用 Switch 和 If else 语句? - When to use Switch and If else statement? 我如何知道何时使用if或switch语句? - How do I know when to use if or switch statement? 根据ESLint,Switch语句默认返回“ Unreachable” - Switch Statement default return “Unreachable” according to ESLint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM