Skip to content

V4 Router methods in props #4005

@porfirioribeiro

Description

@porfirioribeiro

In my project i need to call transitionTo("/some/path") in a function to go to a different location.

That function is available in the router context, why not pass it down as a prop to the matched component?

My solution was to wrap match with the router from the context

import React from 'react';
import {Match, propTypes} from 'react-router';

const CustomMatch = (props, context) => {
  return (
    <Match {...props} render={(matchProps) => {
      const newProps = {...matchProps, router: context.router, ...props};
      if (props.component)
        return React.createElement(props.component, newProps);
      else
        return props.render(newProps);
    }}/>
  );
};

CustomMatch.contextTypes = {
  router: propTypes.routerContext
};
export default CustomMatch;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions