javascript - 在 const 声明中使用 React prop 或 html 元素

标签 javascript reactjs jsx

我创建了一个 Button根据传递的 props 应用各种样式和类的组件。我还希望能够指定另一个 component在诸如 Link 之类的 Prop 中来自react-router 。嵌套会导致各种问题(单击填充不起作用等)。

为此,我会接受 component允许这样做的 prop,但是当未设置此 prop 时,我想使用默认的 html <button>元素。

我经常使用||当使用自定义组件做类似的事情时,但我似乎无法让它与默认的 html 元素一起工作。

class Button extends PureComponent {
    render() {
        const {
            size,
            width,
            variation,
            disabled,
            loading,
            position,
            children,
            className,
            component,
            ...otherProps
        } = this.props;

        // Here lies the problem, "button" is not defined here, how to use the default html element while not loosing props specified below?
        const Button = component || button; 

        return (
            <Button
                className={classnames(
                    "BUTTON",
                    {
                        [`BUTTON-size--${size}`]: size,
                        [`BUTTON-width--${width}`]: width,
                        [`BUTTON-variation--${variation}`]: variation,
                        [`BUTTON-position--${position}`]: position,
                        "BUTTON-state--disabled": disabled,
                        "BUTTON-state--loading": loading
                    },
                    className
                )}
                disabled={disabled || loading}
                {...otherProps}
            >
                {children}
            </Button>
        );
    }
}

最佳答案

查看material-ui-next ButtonBase我注意到这个问题有一个非常简单的方法,即只需执行以下操作:

class Button extends PureComponent {
    render() {
        const {
            size,
            width,
            variation,
            disabled,
            loading,
            position,
            children,
            className,
            component,
            ...otherProps
        } = this.props;

        const Component = component;

        return (
            <Component
                className={classnames(
                    "BUTTON",
                    {
                        [`BUTTON-size--${size}`]: size,
                        [`BUTTON-width--${width}`]: width,
                        [`BUTTON-variation--${variation}`]: variation,
                        [`BUTTON-position--${position}`]: position,
                        "BUTTON-state--disabled": disabled,
                        "BUTTON-state--loading": loading
                    },
                    className
                )}
                disabled={disabled || loading}
                {...otherProps}
            >
                {children}
            </Component>
        );
    }
}

Button.propTypes = {
    component: PropTypes.oneOfType([PropTypes.node, PropTypes.oneOf(["a", "button"])]),
    // ... ohter
};

Button.defaultProps = {
    component: "button",
    // ... other
};

export default Button;

请注意,我只是使用 component 属性,默认值为“button”

关于javascript - 在 const 声明中使用 React prop 或 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534223/

相关文章:

javascript - React - 当输入更改时如何防止重新渲染所有输入字段

javascript - 在React中循环父对象内的对象

javascript - React - 将图标添加到组件内的标题

javascript - 在函数中使用 javascript var - 顺序问题

javascript - 如何将值从 Microsoft Bot 发送到 Javascript?

javascript - 监听某个父元素和某个子元素之间的事件

javascript - 如何使用短路评估在内联条件后渲染多个元素?

javascript - React - 条件 JSX 渲染

javascript - 替换背景图像的平滑方法

javascript - 选择的 JQuery 插件在我的 Ionic 应用程序中不起作用