In react-router, is there any way to pass a property from the Route-definition that can be picked up within the Router.run function? I want specific actions to fire for specific routes. Something like this perhaps:
<Route handler={someComponent} resolve={someAction} />
In Router.Run i want to execute that given action defined in resolve. Is there any way of doing that?
The reason for doing this is to make sure that each route can instantiate service-calls (from a defined action) to make sure that the stores have the data needed for that route. Doing it in the component is also a possibility, the problem we have now is that several components within the same route needs the same data, making it requesting data from the API several times, and also triggering rerendering for each result comming in from those calls.
You can do something like this in React Router by adding a
static
(usingcreateClass
) or a property on the component class (when using ES6 classes) and then executing them with React Router.Then, when you run your router, look for all matched routes that have a
fetchData
static method and call it. We'll assume here thatfetchData
(and thusAPI.getData
) returns a Promise:(See this React Router example for more details.)
You can solve the "multiple components fetch the same data" problem by ensuring that the
API
module will catch requests for the same data and assign them the same promise. Pseudocode: