React Router Onenter Not Working As Intended
I am building a basic react-login app for educational purposes and to authenticate a route I am using React-router's onEnter prop functionality import axios from 'axios'; var
Solution 1:
I can't write the exact example because everyone's implementation differs, but I am going to right a HelloWrapper component example which wraps any component and if it receive the sayHello prop to true, it render the Hello instead of actual component, you can check the login props and redirect or show login component..in your case.
- Write a function to copy props form actual component to wrapper
HoistNonReactStatistics.tsx or jsx ( I use typescript)
constREACT_STATICS= {
childContextTypes:true,
contextTypes:true,
defaultProps:true,
displayName:true,
getDefaultProps:true,
mixins:true,
propTypes:true,
type:true
};constKNOWN_STATICS= {
name:true,
length:true,
prototype:true,
caller:true,
arguments:true,
arity:true
};exportdefaultfunctionhoistStatics(targetComponent,sourceComponent) {
varkeys=Object.getOwnPropertyNames(sourceComponent);for(vari=0;i<keys.length;++i) {
constkey=keys[i];if(!REACT_STATICS[key] &&!KNOWN_STATICS[key]) {
try {
targetComponent[key] =sourceComponent[key];
} catch(error) {}
}
}
returntargetComponent;
}
write wrapper function which will return wrapped component
import * asReactfrom'react'; importHoistNonReactStatisticsfrom"./HoistNonReactStatistics"exportfunctionHelloWrapper(){ returnfunctionHellowWrapComponent(DecoratedComponent) { classWrappendComponentextendsReact.Component<{sayHello?:boolean},{}>{ constructor(props){ super(props); } componentWillMount(){ /** do some login check here and redirect if required **/ } componentWillReceiveProps(nextProps){ /** do some login check here and redirect if required **/ } render(){ if(this.props.sayHello){ return<div>Hello</div> }else{ return<DecoratedComponent /> } } } /** if you use redux then also apply the connect method on return component so it get access to required auth reducer data**/returnHoistNonReactStatistics(WrappendComponent,DecoratedComponent); } }
- create a const ( not sure if this step is required for jsx, however typescript doesn't allow to wrap a component directly because I didn't declare wrapper function to accept arguments
export const requireHellow = HelloWrapper();
- now warp any component with function, I used it at the time of export but you can also do it in Route's component prop
export default requireHellow(SomeComponent);
now whenever you use this SomeComponent and it has sayHello prop to true it will render Hello otherwise render the actual component
Solution 2:
<code>functionrequireAuth(nextState, replace,callback){
var data = {
token: store.getState().student.token,
email: store.getState().student.email}
makeUserRequest('post', data, '/api/auth')
.then((res)=>{
if (!res.data.success) {
replace({ nextPathname: nextState.location.pathname }, nextState.location.query)
}
callback()
})
}
return(
<Routerhistory={browserHistory}><Routepath='/'component={app}><IndexRoutecomponent={login} /><Routepath='home'component={home} /><Routepath='login'component={login} /><Routepath='profile'component={profile}onEnter={requireAuth} /></Route>
)
Post a Comment for "React Router Onenter Not Working As Intended"