React-Router v4 onEnter replacement
My current solution is to put extra code in render function and not to use component property.
<Route exact path="/" render={() => {
getInitialData();
return <Home />;
} } />
I want to suggest a bit different approach. Using class inheritance you can implement your own router.
import React from 'react';
import {Redirect, Route} from 'react-router';
/**
* Class representing a route that checks if user is logged in.
* @extends Route
*/
class AuthRequiredRoute extends Route{
/**
* @example <AuthRequiredRoute path="/" component={Products}>
*/
render() {
// call some method/function that will validate if user is logged in
if(!loggedIn()){
return <Redirect to="/login"></Redirect>
}else{
return <this.props.component />
}
}
}