首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在中使用React体式与@reach/路由器?

如何在中使用React体式与@reach/路由器?
EN

Stack Overflow用户
提问于 2019-02-18 19:28:18
回答 1查看 516关注 0票数 1

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

以下是react-pose中的相关类型

代码语言:javascript
复制
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示例如下所示:

代码语言:javascript
复制
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/

我被困在这里:

代码语言:javascript
复制
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}>上,它说:

类型'ComponentType<{ key: string: any;子键: any;空出?:string欧元-- string[] --未定义;字符串不能指定键入'ComponentFactory>‘。类型'ComponentClass<{ key: string: any;子键: any;空出?:string \ string[]?未定义;_pose?:string欧元- string[],未定义;initialPose?:string欧元string[],未定义;withParent?:布尔值未定义;onPoseComplete?:(体式: string { string[]) => any)未定义;onValueChange?:{.;}未定义;内部.‘不能指定键入'ComponentFactory>‘。类型'ComponentClass<{ key: string: any;子键: any;空出?:string \ string[]?未定义;_pose?:string欧元- string[],未定义;initialPose?:string欧元string[],未定义;withParent?:布尔值未定义;onPoseComplete?:(体式: string { string[]) => any)未定义;onValueChange?:{.;}未定义;内部.‘不提供签名的匹配(poseConfig?:DomPopmotionConfig \ DomPopmotionConfigFactory>未定义):ComponentType<{ key: string: any;ComponentType<?:string \ string[] string[] undefined;_pose?:string string[]未定义;.多.;innerRef?:{.;}string[].1多.未定义;}& PoseContextProps &PoseContextProps&HTMLProps<.>‘>。

这让我很困惑,因为打字看起来是正确的。这里,我不想使用匿名签名,也不想在打字方面表现得很强。

我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2019-06-13 23:15:09

问题是在第一次呈现时,location.key是未定义的。这很可能是为什么你会收到神秘的错误信息,引用一些未定义的东西。我用的是location.pathname。这似乎是没有任何问题的工作。最好使用location.key,但在react-router-dom@5.0.1中,它在第一个呈现时没有定义。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54754086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档