Skip to content

match.params returns empty even when URL contains params #5870

@goodbomb

Description

@goodbomb

Hi, I'm in the process of upgrading to react-router 4 from v2. I'm not sure if this is a bug or not, but I'm having a really hard time getting my Header component to show the correct route params object. It doesn't seem like the match.params object is available globally like the params object was in react-router 2 and I'm not sure how to make it available to sibling components.

App.jsx (wrapped with ConnectedRouter)
-> contains Layout.jsx

const Layout = function(props) {
    return (
        <div className="layout" style={layout.base}>
            <Header inlineStyles={layout.header} params={props.match.params} />
            <Content inlineStyles={layout.content} />
            <Footer inlineStyles={layout.footer} />
        </div>
    );
};

Layout.propTypes = {
    match: PropTypes.object
};

export default Layout;

The Content component is where the bulk of my routes begin. When I try to access props.match inside of the Header component, I see this:

// Header.jsx
match: {
	isExact: false,
	params: {},
	path: "/",
	url: "/"
}

But when I access the props.match object from the nested component route, I get this:

// Content.jsx > Child > GrandChild
match: {
	isExact: true,
	params: {orgId: "1", projectId: "11", version: "1.0.0", modelId: "30"},
	path: "/orgs/:orgId/projects/:projectId/version/:version/models/:modelId",
	url: "/orgs/1/projects/11/version/1.0.0/models/30"
}

This is in the same render cycle.

  1. Is it expected that the match object is not global to the entire router?
  2. How do I access the same match object in my Header.jsx file as I have in my Content.jsx?

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