Причиной возникновения ошибки является то, что состояние помечается как:
type State = {
gameBoard?: Array<Array<Connect4Cell>>
}
?
знак говорит потоку, что это значение может быть неопределенным.
Как и в примере, начальное состояние установлено, и оно содержит gameBoard
в нужной форме, изменения, которые необходимо сделать, это:
type State = {
gameBoard: Array<Array<Connect4Cell>>
}
Однако, если в любое время ожидают, что компонент [live] не установлен gameBoard
, тогда решение будет состоять в том, чтобы добавить проверку перед вызовом функции .map
следующим образом
let board = this.state.gameBoard ? this.state.gameBoard.map(row => <Row />) : null;
You have to be careful how you think about the switch
statement here. There's no creation of variable scopes going on at all, in fact. Don't let the fact that just because the code within cases gets indented that it resides within a child scope.
When a switch block gets compiled, the case
labels are simply converted into labels, and the appropiate goto
instruction is executed at the start of the switch statement depending on the switching expression. Indeed, you can manually use goto
statements to create "fall-through" situations (which C# does directly support), as the MSDN page suggests.
goto case 1;
If you specifically wanted to create scopes for each case within the switch
block, you could do the following.
...
case 1:
{
string x = "SomeString";
...
break;
}
case 2:
{
string x = "SomeOtherString";
...
break;
}
...
This requires you to redeclare the variable x
(else you will receive a compiler error). The method of scoping each (or at least some) can be quite useful in certain situations, and you will certainly see it in code from time to time.
В документации на MSDN говорится:
Область локальной переменной, объявленной в блоке-переключателе оператора switch (раздел 8.7.2), - это переключатель -block.
Кроме того, подобный вопрос задавался ранее: Variable declaration in c# switch statement
Похоже, что область видимости переменных находится внутри коммутатора, а не в случае, вероятно, потому что случаи могут быть объединены в стек. Обратите внимание, что если вы попытаетесь сослаться на x вне переключателя, это не удастся.
if are creating any local variable within case, you can not use them out side case.
...
int opt ;
switch(opt)
{
case 1:
{
string x = "SomeString";
...
}
break;
case 2:
{
string x = "SomeOtherString";
...
}
break;
default:
{
//your code
}
break;
}
...
There is no compiler error because the switch statement does not create a new scope for variables.
If you declare a variable inside of a switch, the variable is in the same scope as the code block surrounding the switch. To change this behavior, you would need to add {}:
...
case 1:
// Start a new variable scope
{
string x = "SomeString";
...
}
break;
case 2:
{
x = "SomeOtherString";
...
}
break;
...
This will cause the compiler to complain. However, switch, on it's own, doesn't internally do this, so there is no error in your code.
move the string declaration to before the
switch(value)
statement. Then assign x for each case.