JMA
JMA

Reputation: 994

onBefore event on react-router

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

Answers (2)

Abdennour TOUMI
Abdennour TOUMI

Reputation: 93223

Yes!

By :

  • encapsulating the redirection logic in one method AND
  • using a library for *event oriented programming like

router-helper.js

import {browserHistory} from 'react-router' ; 
import {fire} from 'mufa';

export function redirect(to) {
     fire('onBeforeRedirect', to);
     browserHistory.push(to);
     fire('onAfterRedirect', to);
}

OtherFile-Subscribe-OnBefore.js

import {on} from 'mufa';

on('onBeforeRedirect', (to) => {
   //Your code here will be running before redirection .
});

Component-Call-Redirect.js

import {redirect} from 'router-helper';


 //...

  redirect('/profile'); //<-- This call will trigger also onBefore listeners

Upvotes: 1

Anis D
Anis D

Reputation: 761

Try to listen to the browser history browserHistory.listen

But first you have to detect the current location with getCurrentLocation

Upvotes: 2

Related Questions