三元运算符 React Native 中的多个条件
Multiple Conditons in ternary operator React Native
大家好,我在 javascript 中有两个使用三元运算符的条件。
这是我的代码,它基本上告诉一个按钮被禁用或启用:
disabled={(props.isAllowedState && (props.zkub == " " || props.zkub == "B")) || (!props.zkub == " " || !props.zkub == "B") ? false : true}
我也这样试过:
disabled={(props.isAllowedState && (props.zkub == " " || props.zkub == "B")) ? false : (!props.zkub == " " || !props.zkub == "B") ? false : true}
但它不起作用,当我分别考虑条件并测试它们时,它起作用但在一起时它们不起作用。
也许有人可以给我提示如何解决这个问题。
褪色
编辑:我的 If 方法
const checkDisableButton = () => {
if(props.isAllowedState && (props.zkub == " " || props.zkub == "B")) {
return false
} if (!props.zkub == " " || !props.zkub == "B") {
return false
} else {
return true
}
};
来电:
disabled={() => checkDisableButton()}
我会创建一个 isDisabled
函数,它使用 if 语句而不是复杂的三元语句。
function isDisabled() {
const { isAllowedState, zkub } = props;
if (isAllowedState && (zkub === ' ' || zkub === 'B')) return false;
if (zkub !== ' ' || zkub !== 'B') return false;
return true;
}
然后调用它:
disabled={isDisabled()}
大家好,我在 javascript 中有两个使用三元运算符的条件。
这是我的代码,它基本上告诉一个按钮被禁用或启用:
disabled={(props.isAllowedState && (props.zkub == " " || props.zkub == "B")) || (!props.zkub == " " || !props.zkub == "B") ? false : true}
我也这样试过:
disabled={(props.isAllowedState && (props.zkub == " " || props.zkub == "B")) ? false : (!props.zkub == " " || !props.zkub == "B") ? false : true}
但它不起作用,当我分别考虑条件并测试它们时,它起作用但在一起时它们不起作用。
也许有人可以给我提示如何解决这个问题。
褪色
编辑:我的 If 方法
const checkDisableButton = () => {
if(props.isAllowedState && (props.zkub == " " || props.zkub == "B")) {
return false
} if (!props.zkub == " " || !props.zkub == "B") {
return false
} else {
return true
}
};
来电:
disabled={() => checkDisableButton()}
我会创建一个 isDisabled
函数,它使用 if 语句而不是复杂的三元语句。
function isDisabled() {
const { isAllowedState, zkub } = props;
if (isAllowedState && (zkub === ' ' || zkub === 'B')) return false;
if (zkub !== ' ' || zkub !== 'B') return false;
return true;
}
然后调用它:
disabled={isDisabled()}