简体   繁体   English

React、样式化组件和 TypeScript:如何将样式化组件包装在功能组件中

[英]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.

相关问题 如何在反应组件中使用样式组件 - How to use styled-components in react component 样式组件未将样式应用于自定义功能反应组件 - styled-components not applying style to custom functional react component Prop TypeScript React 组件错误,使用样式化组件 - Prop TypeScript Error With React Component, Using Styled-Components React, styled-components - 样式内部 styled-component - React, styled-components - style inner styled-component 如何在使用样式组件的 React 组件中测试字符串“...加载”? - How to test for the string '…loading' in React component that uses styled-components? 如何正确使用 ref 与 React class 组件和样式组件? - How to properly use ref with a React class component and styled-components? 如何在 styled-components 中扩展组件样式 - How to extend component style in styled-components 当尝试使用 typescript 定义功能组件以与样式组件反应时,会出现“没有重载匹配此调用”的错误。 - when try to define a functional component in react with styled-components using typescript get error of “No overload matches this call.” 使用Styled-Components,如何在Styled Component外部设置颜色数组? - With a Styled-Components, how to set an array of colors outside of the Styled Component? 如何使用从已有样式组件扩展而来的样式组件? - How to use styled-components that extends from an already styled component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM