我用陣營路由器V4如下做一個非常基本的認證流程:陣營路由器被重定向到登錄頁面,而不是渲染登錄組件
const AuthenticatedRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
sessionStorage.getItem('jwt') ? (
<Component {...props} />
) : (
<Redirect to={{
pathname: "/login",
state: { from: props.location }
}} />
)
)}/>
)
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<BrowserRouter>
<div>
<AuthenticatedRoute exact path="/" component={App} />
<Route path="/login" component={LoginPage} />
</div>
</BrowserRouter>
</Provider>
, document.querySelector('.container'));
流程完美的作品,當沒有「智威湯遜該應用程序被重定向到/登錄。然而,奇怪的是,直到我從瀏覽器中按下刷新,它纔得到渲染(即空白頁)。
不知道如何調試和/或什麼可能是問題,因爲這是一個類似的方法,大多數在線發現的例子。
'LoginPage'組件是否掛載? (在'componentDidMount()'方法中粘貼斷點或日誌消息) –
路由器不負責安裝它嗎? –
你使用react-redux嗎? – Tomasz