简体   繁体   English

如何在React Typescript中与@ reach / router一起使用react-pose?

[英]How to use react-pose with @reach/router in React Typescript?

In my React app I'm trying to convert a JS react-pose - @reach/router sample to TypeScript, but it does not work. 在我的React应用程序中,我试图将JS react-pose @reach/router示例转换为TypeScript,但是它不起作用。

Here are the relating typings in react-pose : 这是react-pose

import { ComponentType, HTMLProps } from 'react';
import { PoseElementProps } from './components/PoseElement/types';
import { DomPopmotionConfig } from 'popmotion-pose';
declare type DomPopmotionConfigFactory<T> = (props: PoseElementProps & T) => DomPopmotionConfig;
export declare type ComponentFactory<T> = (poseConfig?: DomPopmotionConfig | DomPopmotionConfigFactory<T>) => ComponentType<PoseElementProps & T>;
export declare type Posed = {
    <T>(component: ComponentType<T>): ComponentFactory<T>;
    [key: string]: ComponentFactory<HTMLProps<any>>;
};
declare const posed: Posed;
export default posed;

The JS example looks like this: JS示例如下所示:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Link, Location } from '@reach/router';
import posed, { PoseGroup } from 'react-pose';
import Home from './pages/home';
import About from './pages/about';

import './styles.css';

const RouteContainer = posed.div({
  enter: { opacity: 1, delay: 300, beforeChildren: 300 },
  exit: { opacity: 0 }
});

const PosedRouter = ({ children }) => (
  <Location>
    {({ location }) => (
      <PoseGroup>
        <RouteContainer key={location.key}>
          <Router location={location}>{children}</Router>
        </RouteContainer>
      </PoseGroup>
    )}
  </Location>
);

const App = () => (
  <div id="site-container">
    <header>
      <h1>Route transitions with Pose and Reach Router</h1>
    </header>
    <div id="content-container">
      <ul id="site-nav">
        <li>
          <Link to="/">Home</Link>
        </li>
        <li>
          <Link to="/about">About</Link>
        </li>
      </ul>
      <PosedRouter>
        <Home path="/" />
        <About path="/about" />
      </PosedRouter>
    </div>
  </div>
);

ReactDOM.render(<App />, document.getElementById('root'));

Source: https://popmotion.io/pose/examples/route-transitions-reach-router/ 资料来源: https : //popmotion.io/pose/examples/route-transitions-reach-router/

And I'm stuck here: 我被困在这里:

import * as React from 'react';
import { Router, Link, Location } from '@reach/router';
import { ComponentFactory } from 'react-pose/lib/posed';
import posed, { PoseGroup } from 'react-pose';

const RouteContainer: ComponentFactory<React.HTMLProps<any>> = posed.div({
    enter: { opacity: 1, delay: 300, beforeChildren: 300 },
    exit: { opacity: 0 }
});

const PosedRouter: Location = ({ children }: { children: React.ReactNode }) => (
    <Location>
        {({ location }) => (
            <PoseGroup>
                <RouteContainer key={location.key}>
                    <Router location={location}>{children}</Router>
                </RouteContainer>
            </PoseGroup>
        )}
    </Location>
);

On <RouteContainer key={location.key}> it says: <RouteContainer key={location.key}>显示:

Type 'ComponentType<{ [key: string]: any; 输入'ComponentType <{[key:string]:any; children?: any; 孩子们? pose?: string | 姿势?:字符串| string[] | 字符串[] | undefined; 未定义 _pose?: string | _pose ?:字符串| string[] | 字符串[] | undefined; 未定义 initialPose?: string | initialPose ?:字符串| string[] | 字符串[] | undefined; 未定义 withParent?: boolean | withParent ?:布尔值| undefined; 未定义 onPoseComplete?: ((pose: string | string[]) => any) | onPoseComplete ?:((pose:string | string [])=>任何)| undefined; 未定义 onValueChange?: { ...; onValueChange ?: {...; } | } | undefined; 未定义 innerR...' is not assignable to type 'ComponentFactory>'. innerR ...'不可分配给'ComponentFactory>'类型。 Type 'ComponentClass<{ [key: string]: any; 输入'ComponentClass <{[key:string]:any; children?: any; 孩子们? pose?: string | 姿势?:字符串| string[] | 字符串[] | undefined; 未定义 _pose?: string | _pose ?:字符串| string[] | 字符串[] | undefined; 未定义 initialPose?: string | initialPose ?:字符串| string[] | 字符串[] | undefined; 未定义 withParent?: boolean | withParent ?:布尔值| undefined; 未定义 onPoseComplete?: ((pose: string | string[]) => any) | onPoseComplete ?:((pose:string | string [])=>任何)| undefined; 未定义 onValueChange?: { ...; onValueChange ?: {...; } | } | undefined; 未定义 inner...' is not assignable to type 'ComponentFactory>'. 内部...”不能分配给“ ComponentFactory>”类型。 Type 'ComponentClass<{ [key: string]: any; 输入'ComponentClass <{[key:string]:any; children?: any; 孩子们? pose?: string | 姿势?:字符串| string[] | 字符串[] | undefined; 未定义 _pose?: string | _pose ?:字符串| string[] | 字符串[] | undefined; 未定义 initialPose?: string | initialPose ?:字符串| string[] | 字符串[] | undefined; 未定义 withParent?: boolean | withParent ?:布尔值| undefined; 未定义 onPoseComplete?: ((pose: string | string[]) => any) | onPoseComplete ?:((pose:string | string [])=>任何)| undefined; 未定义 onValueChange?: { ...; onValueChange ?: {...; } | } | undefined; 未定义 inner...' provides no match for the signature '(poseConfig?: DomPopmotionConfig | DomPopmotionConfigFactory> | undefined): ComponentType<{ [key: string]: any; 内部...”不提供对签名的匹配'(poseConfig ?: DomPopmotionConfig | DomPopmotionConfigFactory> |未定义):ComponentType <{[[key:string]:any; children?: any; 孩子们? pose?: string | 姿势?:字符串| string[] | 字符串[] | undefined; 未定义 _pose?: string | _pose ?:字符串| string[] | 字符串[] | undefined; 未定义 ... 4 more ...; ...另外4个...; innerRef?: { ...; innerRef ?: {...; } | } | ... 1 more ... | ... 1更多... | undefined; 未定义 } & PoseContextProps & HTMLProps<...>>'. }&PoseContextProps&HTMLProps <... >>'。

This is confusing to me, because the typings look correct. 这使我感到困惑,因为键入看起来正确。 I don't want to use anonymous signatures and stay strong on typing, here. 我不想在这里使用匿名签名并坚持打字。

How can I solve the issue? 我该如何解决这个问题?

The issue is that on the first render location.key is undefined. 问题是在第一个渲染location.key上未定义。 That's likely why you're getting the cryptic error message making reference to something being undefined. 这可能就是为什么您收到隐含的错误消息并引用未定义内容的原因。 I used location.pathname . 我使用location.pathname It seemed to work without any issues. 似乎没有任何问题。 It would be great to use location.key , but in react-router-dom@5.0.1 , it's undefined on the first render. 使用location.key会很棒,但是在react-router-dom@5.0.1 ,它在第一个渲染器上是未定义的。

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

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