I have a requirement to fetch data from a server everytime a page is rendered.
Usually this (ajax) request should be performed inside componentDidMount
, but with ReactRouter, componentDidMount
is not fired when a url has changed.
This is my attempt so far:
// App.js
var App = React.createClass({
render: function() {
return (
<RouteHandler />
);
}
var routes = (
<Route handler={App} path="/" >
...
<Route handler={PageA} name="PageA" path="/pagea" />
...
</Route>
);
Router.run(routes, Router.HashLocation, function(Handler) {
React.render(<Handler/>, document.body);
});
});
// PageA.js
var PageA = React.createClass({
statics: {
willTransitionTo: function (transition, params, query, callback) {
console.log("willTransitionTo PageA");
this.handleGet(callback);
}
, willTransitionFrom: function (transition, component) {
console.log("willTransitionFrom PageA");
}
}
, handleGet(callback) { ... }
...
}
I would like to intercept an event 'page will appear' and willTransitionTo
(and willTransitionFrom
) seems to do the job as expected. However, the problem is I can't access PageA.handleGet
inside that method. I would appreciate if you can guide me to a right path for achieving this task as I might misunderstand some important concepts of using a router.
P.S. I didn't use Flux (yet).