Reputation: 994
Can I call Meteor method before react-router redirect me to a page? I would like to make a history of the logged-in user about his/her page views.
I see that in iron-router I can use onBeforeAction: function () {}
I cant find similar event handler for react-router of ReactTraining. I am searching in their docs, https://github.com/ReactTraining/react-router/tree/1.0.x/docs
In meteor server side, I am using simple:json-routes. I searched also their docs and can't find anything related to onBeforeAction event handler.
I am using react-router so I can't use iron-router at the same time. So Is there any way to log the page views of a logged-in user in Meteor-ReactJS
Upvotes: 1
Views: 772
Reputation: 93223
Yes!
By :
import {browserHistory} from 'react-router' ;
import {fire} from 'mufa';
export function redirect(to) {
fire('onBeforeRedirect', to);
browserHistory.push(to);
fire('onAfterRedirect', to);
}
import {on} from 'mufa';
on('onBeforeRedirect', (to) => {
//Your code here will be running before redirection .
});
import {redirect} from 'router-helper';
//...
redirect('/profile'); //<-- This call will trigger also onBefore listeners
Upvotes: 1
Reputation: 761
Try to listen to the browser history browserHistory.listen
But first you have to detect the current location with getCurrentLocation
Upvotes: 2