首页 文章

React路由器在状态改变后没有重新渲染

提问于
浏览
2

我有这样的反应组件与路由器 . 如果通过props传递的登录名为false,则将呈现登录页面 . 道德是由redux国家推动的 .

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 );

商店创建者应用中间件和Reducer,将历史记录作为参数,因为routerMiddleware需要它 .

之后被调用状态正在改变,记录变为true,我的浏览器中的路径正在改变,历史对象知道该路径改变,但我仍然看到渲染的Login组件和 console.log(logged) 未被第二次调用 .

有谁知道实际上是什么问题?

--EDIT--经过一番调查后,我将这个位代码添加到init:

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

并且第一个日志中的历史记录具有正确的路径,但是在历史记录收到路径名称“/”之后立即进行,这可能是导致问题的原因 .

1 回答

  • 2

    问题出现在 react-redux-routerreact-router 的不兼容版本中 . 我已经更新了第一个,它就像一个魅力 .

相关问题