简体   繁体   English

React Router TypeScript 错误 withRouter

[英]React Router TypeScript errors withRouter

I have two functions, A and Tour.我有两个功能,A 和 Tour。

Function A calls Tour function. Function A 打电话给 Tour function。

interface TourProp {
  isOpen : boolean,
  closeTour : () => void,
}

const A: React.FC<TourProp> = () => {
  const [isTourOpen, SetTourOpen] = useState(false);

  const closeTour = () => {
    SetTourOpen(false);
  };

  const openTour = () => {
    SetTourOpen(true);
  };

  return (
    <div>
      ...
      <Tour isOpen={isTourOpen} closeTour={closeTour} />
    </div>
  );
};

But in the Tour function, if I do this,但是在巡回赛 function 中,如果我这样做,

const Tour : React.FC<TourProp> = withRouter(
  ({isOpen, closeTour, location: { pathname }, history }) => {
    const steps = [
      {
        selector: ".nav",
        content: "This is the Navigation"
      },
      ...(pathname === "/"
        ? [
            {
              selector: ".home",
              content: "This is the Home Content",
              action: () => history.push("/topics")
            }
          ]
         : [
            {
              selector: ".topic1",
              content: "Rendering with React"
            },
            {
              selector: ".topic2",
              content: "Components"
            },
            {
              selector: ".topic3",
              content: "Props v. State"
            }
          ])
       ];
      ....
}

I get the following errors,我收到以下错误,

Property 'isOpen' does not exist on type 'RouteComponentProps<any, StaticContext, unknown> & { children?: ReactNode;属性 'isOpen' 在类型 'RouteComponentProps<any, StaticContext, unknown> & { children?: ReactNode; 上不存在}'. }'。

Property 'closeTour' does not exist on type 'RouteComponentProps<any, StaticContext, unknown> & { children?: ReactNode;类型“RouteComponentProps<any, StaticContext, unknown> & { children?: ReactNode; 上不存在属性“closeTour” }'. }'。

Type 'ComponentClass<Pick<RouteComponentProps<any, StaticContext, unknown>, never>, any> & WithRouterStatics<({ isOpen, closeTour, location: { pathname }, history }: PropsWithChildren<RouteComponentProps<any, StaticContext, unknown>>) => Element>' is not assignable to type 'FC'.输入 'ComponentClass<Pick<RouteComponentProps<any, StaticContext, unknown>, never>, any> & WithRouterStatics<({ isOpen, closeTour, location: { pathname }, history }: PropsWithChildren<RouteComponentProps<any, StaticContext, unknown>>) => 元素 >' 不可分配给类型 'FC'。

Type 'ComponentClass<Pick<RouteComponentProps<any, StaticContext, unknown>, never>, any> & WithRouterStatics<({ isOpen, closeTour, location: { pathname }, history }: PropsWithChildren<RouteComponentProps<any, StaticContext, unknown>>) => Element>' provides no match for the signature '(props: PropsWithChildren, context?: any): ReactElement<any, string |输入 'ComponentClass<Pick<RouteComponentProps<any, StaticContext, unknown>, never>, any> & WithRouterStatics<({ isOpen, closeTour, location: { pathname }, history }: PropsWithChildren<RouteComponentProps<any, StaticContext, unknown>>) => Element>' 没有为签名提供匹配项 '(props: PropsWithChildren, context?: any): ReactElement<any, string | JSXElementConstructor> | JSXElementConstructor> | null'.无效的'。

I am new to TypeScript, any leads on what I am missing?我是 TypeScript 的新手,我缺少什么线索?

you just need to add your type declaration to the withRoute function:你只需要将你的类型声明添加到 withRoute function:

const Tour : React.FC<TourProp> = withRouter<TourProp>(

but note your type must contain the normal withRoute properties like history, location, and the rest if not you would end up have those ones missing to solve that import RouteComponentProps from react-router then use as follows但请注意,您的类型必须包含正常的 withRoute 属性,例如历史记录、位置和 rest,否则您最终会缺少那些来解决从 react-router 导入 RouteComponentProps 然后使用如下

const Tour : React.FC<TourProp> = withRouter<RouteComponentProps<TourProp>>(

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM