【发布时间】:2021-01-28 14:45:29
【问题描述】:
我需要一些帮助,因为我不知道如何使用功能性 React 管理禁用的按钮。
首先,isDisabled 函数持有一个条件,并据此设置按钮状态,但我不确定在哪里调用该函数。该按钮包含一个 disabled 属性,它接收在开始时声明的 disabled 状态。
以下是我目前尝试过的代码:
const MainComponent = ({component}) => {
const [disabled, setDisabled] = useState('');
const isDisabled = () => {
if (component.status === 'disabled') {
setDisabled();
};
}
return (
<div>
<Button>
onClick={createItem}
disabled={disabled}
Button
<Button/>
</div>
)}
如果我的组件状态为禁用,您能否为我提供一些指导并帮助我了解如何使按钮保持禁用状态?
谢谢。
【问题讨论】:
-
props值
<Button> onClick={createItem} disabled={component.status === 'disabled'}Button <Button/>可以直接用相等运算符的结果@ -
我同意@anonymous 的评论,如果您仍有疑问,可以参考codesandbox.io/s/react-functional-component-forked-e4265 进行实施..
标签: javascript reactjs button react-hooks react-functional-component