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

时间:2019-02-18 19:28:19

标签: reactjs typescript react-tsx reach-router react-pose

在我的React应用程序中,我试图将JS react-pose-@reach/router样本转换为TypeScript,但是它不起作用。

以下是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;

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'));

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

我被困在这里:

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>
);

<RouteContainer key={location.key}>上说:

  

Type'ComponentType <{[key:string]:任意;孩子们?姿势?:   字符串字符串[] |未定义_pose ?:字符串|字符串[] |未定义   initialPose ?:字符串|字符串[] |未定义withParent ?:布尔值|   未定义onPoseComplete ?:((pose:string | string [])=>任何)|   未定义onValueChange ?: {...; } |未定义innerR ...'不是   可分配给类型'ComponentFactory>'。类型   'ComponentClass <{[[key:string]:any;孩子们?姿势?:字符串|   字符串[] |未定义_pose ?:字符串|字符串[] |未定义   initialPose ?:字符串|字符串[] |未定义withParent ?:布尔值|   未定义onPoseComplete ?:((pose:string | string [])=>任何)|   未定义onValueChange ?: {...; } |未定义内部...不是   可分配给类型'ComponentFactory>'。       输入'ComponentClass <{[key:string]:any;孩子们?姿势?:字符串|字符串[] |未定义_pose ?:字符串|字符串[] |未定义   initialPose ?:字符串|字符串[] |未定义withParent ?:布尔值|   未定义onPoseComplete ?:((pose:string | string [])=>任何)|   未定义onValueChange ?: {...; } |未定义内部...”不提供   匹配签名'(poseConfig ?: DomPopmotionConfig |   DomPopmotionConfigFactory> |未定义):   ComponentType <{[[key:string]:any;孩子们?姿势?:字符串|   字符串[] |未定义_pose ?:字符串|字符串[] |未定义... 4   更多 ...; innerRef ?: {...; } | ... 1更多... |未定义}&   PoseContextProps和HTMLProps <... >>'。

这使我感到困惑,因为键入看起来正确。我不想在这里使用匿名签名并坚持打字。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题是在第一个渲染ordersRef = FirebaseDatabase.getInstance().getReference().child("Usuarios").child(currentUserID).child("Pedidos"); 上未定义。这可能就是为什么您收到隐含的错误消息并引用未定义内容的原因。我使用了location.key。似乎没有任何问题。使用location.pathname会很棒,但是在location.key中,它在第一个渲染器上是未定义的。