简体   繁体   English

Typescript React/Nextjs styled-components - 将函数作为道具传递给组件会在缺少类型时引发错误

[英]Typescript React/Nextjs styled-components - passing function as prop to component throws error on missing type

I'm new to typescript and struggling with a typescript error while passing down the function "toggle" as a prop to the styled-component "MobileIcon".我是打字稿的新手,在将函数“toggle”作为道具传递给样式组件“MobileIcon”时遇到打字稿错误。 I have set the function type for "toggle" in the interface IProps and added it to the styled-component.我已经在界面 IProps 中为“toggle”设置了函数类型并将其添加到样式组件中。

Layout.tsx:布局.tsx:

import { FunctionComponent as FC, ReactNode, useState } from "react";
import Toolbar from "src/components/Toolbar";

interface IProps {
  main: ReactNode;
}

const Layout: FC<IProps> = ({ main }) => {
  const [isOpen, setIsOpen] = useState<boolean>(false);

  const toggle = () => {
    setIsOpen(!isOpen);
    console.log("toggle ok");
  };

  return (
    <div>
      <nav>
        <Toolbar isOpen={isOpen} toggle={toggle} />
      </nav>
      <main>{main}</main>
    </div>
  );
};

export default Layout;

Toolbar.tsx:工具栏.tsx:

import { FunctionComponent as FC } from "react";
import styled from "styled-components";
import Link from "next/link";

// Components
import Brand from "./Brand";

interface IProps {
  isOpen: boolean;
  toggle: () => void;
}

const Toolbar: FC<IProps> = ({ isOpen, toggle }) => {
  return (
    <Container>
      <Link href="/">
        <a>
          <Brand />
        </a>
      </Link>
      <MobileIcon isOpen={isOpen} onClick={toggle}>...</MobileIcon>
    </Container>
  );
};

export default Toolbar;

styled-component:样式组件:

const MobileIcon = styled.div<IProps>`
...
`

Error:错误:

No overload matches this call.
  Overload 1 of 2, '(props: Omit<Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<...>> & { ...; } & IProps, never> & Partial<...>, "theme"> & { ...; } & { ...; }): ReactElement<...>', gave the following error.
    Property 'toggle' is missing in type '{ children: Element[]; isOpen: boolean; onClick: () => void; }' but required in type 'Omit<Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<HTMLDivElement>> & { ...; } & IProps, never> & Partial<...>, "theme">'.
  Overload 2 of 2, '(props: StyledComponentPropsWithAs<"div", any, IProps, never, "div", "div">): ReactElement<StyledComponentPropsWithAs<"div", any, IProps, never, "div", "div">, string | JSXElementConstructor<...>>', gave the following error.
    Property 'toggle' is missing in type '{ children: Element[]; isOpen: boolean; onClick: () => void; }' but required in type 'Omit<Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<HTMLDivElement>> & { ...; } & IProps, never> & Partial<...>, "theme">'.

Any help would be great.任何帮助都会很棒。

On your JSX, you use your MobileIcon as this:在你的 JSX 上,你使用你的 MobileIcon:

 <MobileIcon isOpen={isOpen} onClick={toggle}>...</MobileIcon>

So the props of MobileIcon here are isOpen and onClick.所以这里MobileIcon的props是isOpen和onClick。 You don't have a toggle prop.你没有切换道具。

Your MobileIcon props interface is then:您的 MobileIcon 道具界面是:

interface MobileIconProps = {
   isOpen: boolean;
   onClick: () => void;
}

And can be used as:并且可以用作:

const MobileIcon = styled.div<MobileIconProps>

Your IProps specifies a toggle prop.您的IProps指定了一个toggle道具。 Therefore your MobileIcon component also expects that prop, yet you don't specify it in your Toolbar component.因此,您的MobileIcon组件也需要该道具,但您没有在Toolbar组件中指定它。

Perhaps you didn't want your MobileIcon to use the props specified by IProps , but just {} (no props) instead.也许你不希望你的MobileIcon使用由指定的道具IProps ,只是{}无道具)来代替。 After all, a div doesn't have isOpen or toggle .毕竟, div没有isOpentoggle

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

相关问题 Typescript React/styled-components - 将 function 作为组件传递给组件会在缺少类型时引发错误 - Typescript React/ styled-components - passing function as prop to component throws error on missing type Prop TypeScript React 组件错误,使用样式化组件 - Prop TypeScript Error With React Component, Using Styled-Components 将prop传递给样式组件的TypeScript错误 - TypeScript error passing prop to styled-components 将方向道具传递给 styled-components 关键帧组件? - Passing a direction prop into styled-components keyframe component? 使用带有打字稿的样式组件的“as”多态属性 - Using 'as' polymorphic prop of styled-components with typescript 使用Material-UI组件的styled-components typescript错误 - styled-components typescript error with Material-UI component Nextjs 中带有组件的样式化组件 - Styled-components with components in Nextjs React、样式化组件和 TypeScript:如何将样式化组件包装在功能组件中 - React, styled-components and TypeScript: How to wrap a styled component in a functional component 打字稿说我用样式组件包装的组件上不存在道具 - Typescript says a prop does not exist on a component I wrapped with styled-components 创建一个 Typescript/React/Styled-Components 按钮 - Creating a Typescript/React/Styled-Components button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM