[英]React styled: Wrapper component produces `Using kebab-case for css properties in objects is not supported. Did you mean ariaLabel?` error
使用情感/风格 v11
为了更改项目中所有按钮的默认type
,我为 HTML <button>
编写了一个包装器组件WrappedButton
。 为了让这个新的WrappedButton
组件具有styled
( styled(WrappedButton)({...})
),我需要用styled
包装我的WrapperButton
。
尝试在我的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?
当我将aria-label
更改为ariaLabel
时,没有错误,但随后未设置 label。
如何在保持用例完好无损的同时消除错误?
包裹按钮
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';
用法
test('A', () => {
render(<WrappedButton aria-label='foo'>a</WrappedButton>);
});
shouldForwardProp
const StylableButton = styled('button',
{
//shouldForwardProp: (prop) => (prop !== 'aria-label')
}
)({}, (props: ButtonPropsType) => ({
shouldForwardProp: (prop) => (prop !== 'aria-label'),
...(props as any),
}));
从vighnesh153的评论中得出:
解决方案是从StylableButton
的定义中删除对props
的提及:
const StylableButton = styled.button();
显然,一切都在幕后按预期进行,更不用说道具了。
这是完整的来源:
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.