Ruben Martinez Jr.
Ruben Martinez Jr.

Reputation: 3110

Pass Props Between Routes Using React-Router

Is the only way to pass data between routes in React-Router (1.0.0-rc) url params? I have a component, A, which uses the History mixin, and has an event handler that makes a server request and then calls that.history.pushState(null, '/B'); to transition to route B handled by component B. Now, I would like to pass some of the data I am returned by the server to component B as a prop (e.g. "login successful"), or somehow affect the state of B, but I can't find any documentation stating this is possible. Is there any way to do this, or do I need to pass it as a url parameter?

Upvotes: 5

Views: 2596

Answers (2)

sundar
sundar

Reputation: 139

Use localstorage or sessionsessionstorage. Set data in first router, get it on second router using set and get item of localstorage

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Upvotes: 0

Clarkie
Clarkie

Reputation: 7550

You have two options:

  1. pass the data as a query params
  2. keep the state of the application somewhere else, outside of the routes

For #2 you could use something like redux which will keep a 'global' store of the application state. You can then access this state from both component A and B.

Upvotes: 5

Related Questions