[英]React, styled-components and TypeScript: How to wrap a styled component in a functional component
I'm trying to create a wrapper for my styled component but I'm having trouble getting the types right.我正在尝试为我的样式组件创建一个包装器,但我无法正确选择类型。
Let's say I have a styled component like this:假设我有一个这样的样式组件:
const Button = styled.button<ButtonProps>`
background-color: ${(props) => props.color};
`;
Now I want to create a wrapper component that contains this styled button, eg.现在我想创建一个包含此样式按钮的包装器组件,例如。 like this:
像这样:
const WrappedButton: React.FunctionComponent<ButtonProps> = ({ children, ...rest }) => (
<div>
<Button {...rest}>{children}</Button>
</div>
);
Now this all works fine, but what I actually want is the WrappedButton
component to accept all props that the Button
component would accept and pass them along to the wrapped Button
component .现在这一切正常,但我真正想要的是
WrappedButton
组件接受Button
组件将接受的所有道具并将它们传递给包装的Button
组件。
So for example I want this to compile, as type is a valid prop of a HTML button element (and therefore also a valid prop of the Button component, but not when the Button component is wrapped):因此,例如,我希望编译它,因为 type 是 HTML 按钮元素的有效道具(因此也是 Button 组件的有效道具,但不是在包装 Button 组件时):
// TypeScript will throw an error because "type" is not a valid prop of WrappedButton.
const MyComponent = () => <WrappedButton type="submit">Test</WrappedButton>
I know I can make "type" a prop of the WrappedComponent, but that's not the point, I want the WrappedComponent to accept all props that a normal HTML button would accept .我知道我可以“输入” WrappedComponent 的道具,但这不是重点,我希望 WrappedComponent接受普通 HTML 按钮可以接受的所有道具。
EDIT: Also I need all styled-component specific props on the wrapped component, such as the as
prop of styled-components.编辑:我还需要包装组件上的所有样式组件特定道具,例如样式组件的
as
道具。 Here's an updated version of the code sandbox: https://codesandbox.io/s/react-typescript-styled-components-forked-3o20j?file=/src/index.tsx这是代码沙箱的更新版本: https://codesandbox.io/s/react-typescript-styled-components-forked-3o20j?file=/src/index.tsx
I have tried so many things but TypeScript is always complaining.我尝试了很多东西,但 TypeScript 总是在抱怨。 I have also searched the docs and the internet but have not found anything.
我也搜索了文档和互联网,但没有找到任何东西。
I believe You are asking about React.ButtonHTMLAttributes<HTMLButtonElement>
.我相信你在问
React.ButtonHTMLAttributes<HTMLButtonElement>
。
import React from 'react'
import styled from 'styled-components'
const Button = styled.button<ButtonProps>`
background-color: ${(props) => props.color};
`;
type ButtonProps = {
} & React.ButtonHTMLAttributes<HTMLButtonElement>;
const WrappedButton: React.FunctionComponent<ButtonProps> = ({ children, ...rest }) => (
<div>
<Button {...rest}>{children}</Button>
</div>
);
If you want to use raw html <button>
, you mignt be interested in: JSX.IntrinsicElements["button"];
如果您想使用原始 html
<button>
,您可能会对以下内容感兴趣: JSX.IntrinsicElements["button"];
Check this answer检查这个答案
UPDATE The quick and dirty solution would be:更新快速而肮脏的解决方案是:
type ButtonProps = {
} & React.ButtonHTMLAttributes<HTMLButtonElement> & { as?: string | React.ComponentType<any> };
But it is not generic.但它不是通用的。
There is StyledComponentPropsWithAs
type in SC typings, but it is not exported ( SC类型中有
StyledComponentPropsWithAs
类型,但没有导出(
If you're trying to suppress native attributes you could do something like this:如果您试图抑制本机属性,您可以执行以下操作:
interface Props {
type?: 'error' | 'success'
}
type OmitNativeAttrs = Omit<React.HTMLAttributes<HTMLButtonElement>, keyof Props>
export type ButtonProps = Props & OmitNativeAttrs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.