状态更改后反应路由器不重新渲染

React router not rerendering after state change

我有这样的带有路由器的反应组件。如果 props 传递的登录信息为假,那将呈现登录页面。 Props 由 redux state 提供燃料。

const Root = ({logged, history}) => {
  console.log(logged);
  return (
    <div className="App">
      <Router history={history}>
        <div>
          <Route exact path="/" component={ATM} />
          <Route path="/login" component={LoginForm}/>
        </div>
      </Router>
      //Removed without effect-->{!logged ? <Redirect to="/login"/> : ""}
    </div>
  )
};

const fromState = (state) => ({
  logged: state.auth.logged
});

export default connect(fromState)(Root)

在我的登录组件中,我破解了一个调用,该调用将询问服务器用户是否已成功授权,如果是的话则重定向。

this.props.dispatch(action).then((result) => {
  let responseCode = result.payload.status;
  if(responseCode === 200) {
    this.props.dispatch(push("/"))
  }
})

在我的应用程序中,我这样定义历史记录:

let blankHistory = createHistory();
let store = storeCreator(blankHistory);
const history = syncHistoryWithStore(blankHistory,store );

Store creator 应用中间件和 reducer,将 history 作为参数,因为 routerMiddleware 需要它。

之后调用状态发生变化,记录变为真,我浏览器中的路径发生变化,历史对象知道该路径变化,但我仍然看到渲染的登录组件并且 console.log(logged) 没有被调用第二次.

有谁知道真正的问题是什么?

--编辑-- 经过一番调查后,我将这段代码添加到 init:

let history = syncHistoryWithStore(blankHistory,store );
console.log(history.location)
history.listen((something) => {
  console.log(something)
});

第一个日志中的历史记录具有正确的路径,但在那之后历史记录立即收到路径名:“/”,这可能是问题的原因。

问题出在 react-redux-routerreact-router 的不兼容版本上。我已经更新了第一个,它非常有用。