[英]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
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
组件具有styled
( styled(WrappedButton)({...})
),我需要用styled
包装我的WrapperButton
。
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。
How can I get rid of the error while keeping my use cases intact?如何在保持用例完好无损的同时消除错误?
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>);
});
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.