[英]How can I add conditions to my input in react?
Take a look at this example.看看这个例子。
export const Foo = () => {
const [state, setState] = useState(0)
return (
<div>
<input type="button" value="" {state === 0 ? disabled : ""}/>
</div>
)
}
I'm trying to make the button disabled if state
is equal to 0 and if not then the button will not be disabled.如果
state
等于 0,我正在尝试禁用按钮,如果不是,则不会禁用按钮。 I don't know what is wrong and why it gives me this error: '...' expected
我不知道出了什么问题以及为什么它给了我这个错误:
'...' expected
react thinks you are trying to pass props. react 认为您正在尝试传递道具。 try this:
尝试这个:
disabled={state} when state is true/false instead of 0/1
or或者
disabled={state === 0}
Can you try:你能试一下吗:
<input type="button" value="" disabled = {(state)? "disabled" : "" }/>
You must set "disabled" like if was a prop.您必须像道具一样设置“禁用”。
React is expecting props
that's why it is giving '...' expected
. React 期待
props
,这就是为什么它给出'...' expected
。
what you can do is disable the button when the state is 0
.您可以做的是在 state 为
0
时禁用该按钮。 codesandbox 密码箱
<input type="button" value="insert number" disabled={state === 0} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.