简体   繁体   English

React 样式:Wrapper 组件生成`不支持将 kebab-case 用于对象中的 css 属性。 您是说 ariaLabel?` 错误

[英]React styled: Wrapper component produces `Using kebab-case for css properties in objects is not supported. Did you mean ariaLabel?` error

Using emotion/styled v11使用情感/风格 v11

Background背景

In order to change the default type of all buttons in my project, I wrote a wrapper component WrappedButton for the HTML <button> .为了更改项目中所有按钮的默认type ,我为 HTML <button>编写了一个包装器组件WrappedButton In order to allow this new WrappedButton component to be styled ( styled(WrappedButton)({...}) ), I needed to wrap my WrapperButton with styled .为了让这个新的WrappedButton组件具有styledstyled(WrappedButton)({...}) ),我需要用styled包装我的WrapperButton

Problem问题

When trying to set the aria-label attribute on my WrappedButton I get the console error Using kebab-case for css properties in objects is not supported. Did you mean ariaLabel?尝试在我的WrappedButton上设置aria-label属性时,出现控制台错误Using kebab-case for css properties in objects is not supported. Did you mean ariaLabel? Using kebab-case for css properties in objects is not supported. Did you mean ariaLabel?

When I change aria-label to ariaLabel , there's no error, but then the label is not set.当我将aria-label更改为ariaLabel时,没有错误,但随后未设置 label。

Question问题

How can I get rid of the error while keeping my use cases intact?如何在保持用例完好无损的同时消除错误?

Code代码

WrappedButton包裹按钮


type ButtonPropsType = React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>;
type RefType = ((instance: HTMLButtonElement | null) => void) | React.RefObject<HTMLButtonElement> | null | undefined;

/** 
  * This is needed in order to allow the button to be styled by
  * emotion (`styled(WrappedButton)({...})`
 **/
const StylableButton = styled.button({}, (props: ButtonPropsType) => ({
    ...(props as any),
}));

// change default `type` from `submit` to `button`
const defaultProps: ButtonPropsType = {
    type: 'button',
};

export const WrappedButton = forwardRef((props: ButtonPropsType, ref: RefType) => {
    return <StylableButton {...defaultProps} ref={ref} {...props} />;
});
WrappedButton.displayName = 'Button';

Usage用法

test('A', () => {
    render(<WrappedButton aria-label='foo'>a</WrappedButton>);
});

What I've tried:我试过的:

shouldForwardProp shouldForwardProp

const StylableButton = styled('button',
  {
  //shouldForwardProp: (prop) => (prop !== 'aria-label')
  }
)({}, (props: ButtonPropsType) => ({
    shouldForwardProp: (prop) => (prop !== 'aria-label'),
    ...(props as any),
}));

Figured out from vighnesh153 's comment:vighnesh153的评论中得出:

The solution is to remove mentioning of props from the definition of StylableButton :解决方案是从StylableButton的定义中删除对props的提及:

const StylableButton = styled.button();

Apparently, everything works as expected already behind the scenes without mentioning the props.显然,一切都在幕后按预期进行,更不用说道具了。

Here's the full source:这是完整的来源:

type ButtonPropsType = React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>;
type RefType = ((instance: HTMLButtonElement | null) => void) | React.RefObject<HTMLButtonElement> | null | undefined;

/** 
  * This is needed in order to allow the button to be styled by
  * emotion (`styled(WrappedButton)({...})`
 **/
const StylableButton = styled.button();

// change default `type` from `submit` to `button`
const defaultProps: ButtonPropsType = {
    type: 'button',
};

export const WrappedButton = forwardRef((props: ButtonPropsType, ref: RefType) => {
    return <StylableButton {...defaultProps} ref={ref} {...props} />;
});
WrappedButton.displayName = 'Button';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM