react-router-dom with TypeScript

前端 未结 7 574
清歌不尽
清歌不尽 2021-01-30 19:56

I\'m trying to use react router with TypeScript. However, I have certain problems using withRouter function. On the last line, I\'m getting pretty weird error:

A         


        
相关标签:
7条回答
  • 2021-01-30 20:33

    I use a different approach to fix this. I always separate the different properties (router, regular and dispatch), so I define the following interfaces for my component:

    interface HomeRouterProps {
      title: string;   // This one is coming from the router
    }
    
    interface HomeProps extends RouteComponentProps<HomeRouterProps> {
      // Add your regular properties here
    }
    
    interface HomeDispatchProps {
      // Add your dispatcher properties here
    }
    

    You can now either create a new type that combines all properties in a single type, but I always combine the types during the component definition (I don't add the state here, but if you need one just go ahead). The component definition looks like this:

    class Home extends React.Component<HomeProps & HomeDispatchProps> {
      constructor(props: HomeProps & HomeDispatchProps) {
        super(props);
      }
    
      public render() {
        return (<span>{this.props.match.params.title}</span>);
      }
    }
    

    Now we need to wire the component to the state via a container. It looks like this:

    function mapStateToProps(state, ownProps: HomeProps): HomeProps => {
      // Map state to props (add the properties after the spread)
      return { ...ownProps };
    }
    
    function mapDispatchToProps(dispatch): HomeDispatchProps {
      // Map dispatch to props
      return {};
    }
    
    export default connect(mapStateToProps, mapDispatchToProps)(Hello);
    

    This method allows a fully typed connection, so the component and container are fully typed and it is safe to refactor it. The only thing that isn't safe for refactoring is the parameter in the route that is mapped to the HomeRouterProps interface.

    0 讨论(0)
  • 2021-01-30 20:44

    This is a Typescript typings issue. Since withRouter() will provide the routing props you need at runtime, you want to tell consumers that they only need to specify the other props. In this case, you could just use a plain ComponentClass:

    export default withRouter(connectModule) as React.ComponentClass<{}>;
    

    Or, if you had other props (defined in an interface called OwnProps) you wanted to pass in, you could do this:

    export default withRouter(connectModule) as React.ComponentClass<OwnProps>;
    

    There is slightly more discussion here

    0 讨论(0)
  • 2021-01-30 20:49

    The way how I'm doing this:

    interface IOwnProps {
      style?: CSSProperties;
    }
    
    interface INavProps {
      item?: string;
    }
    
    type IProps = IOwnProps & RouteComponentProps<INavProps>;
    
    export default class MapScreen extends PureComponent<IProps> {
      ...
    }
    
    0 讨论(0)
  • 2021-01-30 20:52

    It looks like you have the right usage to apply the match, history, and location props to your component. I would check in your node_modules directory to see what versions of react-router and react-router-dom you have, as well as the @types modules.

    I have essentially the same code as you, and mine is working with the following versions:

    {
      "@types/react-router-dom": "^4.0.4",
      "react-router-dom": "^4.1.1",
    }
    
    0 讨论(0)
  • 2021-01-30 20:58

    +1 for Ramon's answer. You can absolutely get full type coverage here. To add a bit - I added the call to withRouter.

    interface FooProps extends RouteComponentProps<Foo> {
      foo: string;
    }
    
    const Foo = ({ history, foo }: FooProps) => <span/>;
    
    const RoutedFoo = withRouter(Foo);
    

    my dependencies:

    "@types/react-router-dom": "^4.3.0",
    "typescript": "^2.9.2",
    
    0 讨论(0)
  • 2021-01-30 20:59

    I think it is a typescript typings compilation issue, but I've found a workaround:

    interface HomeProps extends RouteComponentProps<any>, React.Props<any> {
    }
    
    0 讨论(0)
提交回复
热议问题