Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 667
Ruben Martinez Jr. Avatar asked Sep 16 '15 18:09

Ruben Martinez Jr.


People also ask

How do I pass a component in react router?

If you are using react-router v4, you can pass it using the render prop. Sometimes you need to render whether the path matches the location or not. In these cases, you can use the function children prop. It works exactly like render except that it gets called whether there is a match or not.

How do I access react router props?

Another way to access the router props is to add an import statement at the top of the component and import 'withRouter'. import { withRouter } from 'react-router-dom'; Then in the export default statement at the end of the component you would wrap the component in 'withRouter'.


1 Answers

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.

like image 72
Clarkie Avatar answered Sep 26 '22 01:09

Clarkie