Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React中的条件句_Javascript_Reactjs - Fatal编程技术网

Javascript React中的条件句

Javascript React中的条件句,javascript,reactjs,Javascript,Reactjs,如何在React中使用条件句 基本上,对于CustomerClassesMaining,我现在说的是“类”,但我想写一段代码,如果有一个类,就用“类”,如果有多个类,就用“类”。谢谢 render(){ 常量加载框=( Array.from({length:20}).map((n,i)=>( )) ); const customerbox=!this.props.customerslessthan五个?加载框 :this.props.customersLessThanFive.map((客户,i

如何在React中使用条件句

基本上,对于CustomerClassesMaining,我现在说的是“类”,但我想写一段代码,如果有一个类,就用“类”,如果有多个类,就用“类”。谢谢

render(){
常量加载框=(
Array.from({length:20}).map((n,i)=>(
))
);
const customerbox=!this.props.customerslessthan五个?加载框
:this.props.customersLessThanFive.map((客户,i)=>(
if(customer.classes_剩余==1){
customerClassesRemaining2={`${customer.classes\u剩余}类左`}
}
否则{
customerClassesRemaining2={${customer.classes\u剩余的}类剩余`}
}
))
您可以试试

customerClassesRemaining2 = customer.classes_remaining == 1 ? `${customer.classes_remaining} class left` : `${customer.classes_remaining} classes left`;
你可以试试

customerClassesRemaining2 = customer.classes_remaining == 1 ? `${customer.classes_remaining} class left` : `${customer.classes_remaining} classes left`;

使用三元运算符,如:

customerClassesRemaining={`${customer.classes_remaining} class${customer.classes_remaining > 1 ? 'es' : ''} left`}

使用三元运算符,如:

customerClassesRemaining={`${customer.classes_remaining} class${customer.classes_remaining > 1 ? 'es' : ''} left`}

谢谢!为什么我不能使用if-else语句?我是全新的反应者,所以我仍在熟悉它。如果您想在模板文本
(```)中编写条件
你应该使用三元运算符。你也可以使用if-else语句,但在另一个地方谢谢!为什么我不能使用if-else语句?我对react是全新的,所以我仍然很熟悉它。如果你想在模板文本中编写条件
(````)
您应该使用三元运算符。您也可以使用if-else语句,但在另一个位置使用。那么问题出在哪里?请修复它,以免导致语法错误(因为存在一些错误)并按您的意愿使用CustomerClassesRemaining 2。@EstusFlask我使用三元运算符修复了代码,但我只是想知道为什么不能使用上述if-else语句。语法错误是什么?我刚刚开始学习React。您的问题不是特定于React的。您可以使用if-else,只要它是有效的JS。有两个问题,customerClassesRemaining2变量未声明,
={`${customer.classes\u remaining}class left`}
不是有效的JS语法。括号是JSX语法,只需要在React组件中使用。那么问题出在哪里呢?请修复它,以免导致语法错误(因为存在一些错误)并按您的意愿使用CustomerClassesRemaining 2。@EstusFlask我使用三元运算符修复了代码,但我只是想知道为什么不能使用上述if-else语句。语法错误是什么?我刚刚开始学习React。您的问题不是特定于React的。您可以使用if-else,只要它是有效的JS。有两个问题,customerClassesRemaining2变量未声明,
={`${customer.classes\u remaining}class left`}
不是有效的JS语法。括号是JSX语法,仅在React组件中需要。