0
いくつかの権限が必要なページにアクセスしようとすると、ユーザーをサインインページにリダイレクトしようとしています。基本的には、/
ルートにはユーザがログインする必要があります。そうでない場合は/signin
にリダイレクトします。react-router-dom <Redirect />はsyncHistoryWithStoreと連携していませんか?
react-router-reduxのsyncHistoryWithStoreを使用すると、リダイレクトは行われません。私はそれを削除すると、期待どおりに動作します。
syncHistoryWithStoreはreact-router-domの最新バージョンと互換性がありませんか?
ここにはreproductionです。 syncHistoryWithStoreを使用しない履歴行のコメントを解除し、期待通りに動作することを確認できます。
また、ここにコードを組み込んでください。
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, combineReducers } from 'redux';
import { createBrowserHistory } from 'history';
import { Router, Route, Redirect } from 'react-router-dom';
import { syncHistoryWithStore, routerReducer } from 'react-router-redux';
const store = createStore(
combineReducers({
routing: routerReducer
})
);
const history = syncHistoryWithStore(createBrowserHistory(), store);
//const history = createBrowserHistory();
function SignIn(){
return (
<div>This is SignIn.</div>
);
}
function Home(){
return (
<div>This is Home.</div>
);
}
const isLoggedIn = false;
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<div>
<Route exact path="/signin" component={SignIn} />
<Route
exact
path="/"
render={() => isLoggedIn ? <Home /> : <Redirect to="/signin" />}
/>
</div>
</Router>
</Provider>,
window.document.getElementById('root')
);
react-router-reduxはreact-router v4をサポートしていません。 readmeのhttps://github.com/reactjs/react-router-redux –
を参照してください。私はその小さな文に気付かなかったすべての情報に圧倒されました。 – Gabin