【发布时间】:2023-02-07 17:46:47
【问题描述】:
这是我的代码
import React from 'react'
import PropTypes from "prop-types";
export default function Button({ htmlType, type, disabled, action, ...props}) {
return (
<button type={htmlType} onClick={action}>
{props.children}
</button>
)
}
Button.propTypes = {
htmlType: PropTypes.string.isRequired,
action: PropTypes.func,
disabled: PropTypes.bool
};
我通过这段代码调用 Button 组件
<Button disabled={true}>button title</Button>
我想在 disabled of props 为真时向按钮添加 disabled html 属性,该怎么做?
【问题讨论】:
-
您实际上需要将禁用属性添加到真实按钮:
return <button type={htmlType} onClick={action} disabled={disabled}>{props.children}</button>。 -
只需将禁用传递给按钮。像这样:<button disabled={Boolean(disabled)} ...
标签: html reactjs react-props