简体   繁体   中英

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 (eg "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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM