[英]multiple condition in ternary operator in jsx
<div style={{'backgroundColor': status === 'approved' ? 'blue' : 'black'}}>
</div>
black is the default color but what if I want to add the 3rd condition?黑色是默认颜色,但如果我想添加第三个条件怎么办?
status can be 'approved', 'rejected', 'pending' or more.状态可以是“已批准”、“已拒绝”、“待定”或更多。
You could do the following:您可以执行以下操作:
<div style={{'backgroundColor': status === 'approved' ? 'blue' : status === 'pending' ? 'black' : 'red'}}>
</div>
This means if status === 'approved'
set the background color as blue, if status === 'pending'
set it as black, else set it as red.这意味着如果
status === 'approved'
将背景颜色设置为蓝色,如果status === 'pending'
将其设置为黑色,否则将其设置为红色。
I would suggest using functions if your conditions get complicated, to not degrade your code readability.如果您的条件变得复杂,我建议您使用函数,以免降低代码的可读性。
getBackgroundColor(status) {
if (status === 'approved') {
return 'blue';
}
if (status === 'pending') {
return 'red';
}
return 'black';
}
render() {
// ...
return (
<div style={{ 'backgroundColor': this.getBackgroundColor(status) }}></div>
);
}
Using multiple ternary operators is not a good idea, better to use a function and put if-else conditions inside that and call that function from render.使用多个三元运算符不是一个好主意,最好使用一个函数并将 if-else 条件放在其中并从渲染调用该函数。 It helps you to make the render part clean and short.
它可以帮助您使渲染部分干净而简短。
Like this:像这样:
<div style={{'backgroundColor': this._style(status)}}></div>
_style(status){
if(status == 'approved')
return 'blue';
else if(status == 'pending')
return 'black';
else return 'red';
}
I'd handle it separately as other types of status may appear in the future.我会单独处理它,因为将来可能会出现其他类型的状态。
const getBackgroundColor(status) {
if (status === 'approved') {
return 'blue'
}
else if (status === 'pending') {
return 'black'
} else {
return 'red'
}
}
<div style={{'backgroundColor': getBackgroundColor(status) }}>
</div>
Code gets easier to understand and reason about.代码变得更容易理解和推理。
To chain ternary operations you need to add another ternary operator to be returned when the conditions are not met, for example:要链接三元运算,您需要添加另一个三元运算符以在不满足条件时返回,例如:
a === true ? a : b
In place of b
you would add a new ternary operator, like so:代替
b
您将添加一个新的三元运算符,如下所示:
a === true ? a : b === true ? b : c
Bonus:奖金:
When you're just checking for null/undefined/false you can use the pipe operator, for example this:当您只是检查 null/undefined/false 时,您可以使用管道运算符,例如:
var x = a !== null ? a : b;
Can be simplified to:可以简化为:
var x = a || b;
And pipe operators can be chained infinitely like ternary operators.管道运算符可以像三元运算符一样无限链接。
There is another way how to do it with the a bit more readable & cleaner code style.还有另一种方法可以使用更具可读性和更清晰的代码风格来做到这一点。 We can replace the ternary operator with the object literal and use this instead of nesting ternary operators, like so
我们可以用对象字面量替换三元运算符,并使用它代替嵌套三元运算符,就像这样
function getBackgroundColor(status){
const backgroundColorByStatus = {
approved: 'blue',
pending: 'black',
default: 'red',
}
return backgroundColorByStatus[status] || backgroundColorByStatus['default']
}
// somewhere below
<div style={{'backgroundColor': getBackgroundColor(status)}}>fancy div</div>
With this approach you can have multiple colors and code will be still clean & readable :)使用这种方法,您可以拥有多种颜色,并且代码仍然干净且可读:)
Hope it will help.希望它会有所帮助。
JSX
和JS
中三元运算符中的多个条件
style={{'backgroundColor': status === 'approved' ? 'blue' : status === 'cancel' ? 'red' : 'green'}}
I would not use ternary because it gets hard to read.我不会使用三元,因为它很难阅读。 Why not store the status and associated colors in an object then just reference that?
为什么不将状态和相关颜色存储在一个对象中,然后只引用它呢?
const colors = {approved:"blue", rejected:"red"};
<div style={{'backgroundColor':status in colors ? colors[status] : "black"}}>
</div>
Oops, I didn't realize how old this thread was.哎呀,我没有意识到这个线程有多老了。
Inside render you can create an empty array variable.在渲染内部,您可以创建一个空数组变量。 As shown below, you can apply nested styling.
如下所示,您可以应用嵌套样式。 Also, you won't need a nested ternary operator.
此外,您不需要嵌套的三元运算符。
let styleValue = [];
if(status === 'approved') {
styleValue.push({backgroundColor:'blue'})
} else {
styleValue.push({backgroundColor:'black'})
}
<div style={styleValue}>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.