history.push()在反应中是异步调用吗?

时间:2018-12-25 10:33:34

标签: reactjs react-router

我正在使用我的化简器中的react提供的历史对象(从组件传递到化简器)在更新状态时有条件地路由到其他页面。 我知道那不是必须处理导航的方式。 但它仍在工作,history.push()函数是否像我一样异步调用 在reducer中返回修改状态之前执行它。 有人可以解释一下为什么这样

const reducer = (state = initialState, action) => {
var stateClone = {...state};
switch(action.type)
{
  case 'LogIn':
  var user = state.users.filter((user) => user.userName === action.userName && user.password === action.password );
  if(user.length > 0 )
  {
    var loggedInUser = {...user[0]};
    stateClone.loggedInUser = loggedInUser;
    action.history.push('/products');
    return stateClone;
  }
  else {
    alert('Wrong Credentials');
  }
  return state;
}
}

1 个答案:

答案 0 :(得分:2)