javascript - React Router Cannot Get / depending on onEnter function -
i new react , hoping can shed light on why happening , how debug it.
i have following routes defined:
export default (withhistory, onupdate) => { const history = withhistory? (modernizr.history ? new browserhistory : new hashhistory) : null; return ( <router history={history}> <route path='/login' component={login} /> <route path='/' component={home} onenter={requireauth} /> </router> ); }; requireauth supposed check if user logged in , redirect them login page if not:
function requireauth(nextstate, transition) { transition.to("/login"); } if leave transtion.to call in , browse root url see message says, "cannot /" no error messages in debugger. putting breakpoint on line doesn't seem anything.
what's odd if replace transition.to(...) debugger; statement, method called , routing works fine.
i must have misunderstanding on something, ideas on is?
edit: should add navigating host:port/login works fine, know login route works.
coming angular background had incorrectly assumed routing happening entirely client-side. it's possible use react-router both client-side and server side routing.
the transition.to call worked fine on client-side, threw exception when server-side routing kicked in , returned cannot / page. exception being caught nothing logged.
i added if statement see if running on client:
if(window.location) { if(!loggedin) { transition.to("/login"); } } there better way handle , if figure out i'll update answer.
Comments
Post a Comment