React - 更新下一个状态并在reducer中重定向

时间:2017-10-27 09:27:38

标签: reactjs react-router

是React的新手,现在几天都在尝试做出反应。只想知道如何更新新状态并重定向到新路由。我正在尝试这样的事情:

Object.assign({}, state, {client: action.client});
Router.browserHistory.push('/edit-client?id='+action.client.client_id);

但是组件注意到没有用新状态更新。

reduce.js

const reducer = (state = initial, action) => {
  switch (action.type) {    
    case EDIT_CLIENT:
      Object.assign({}, state, {client: action.client});
      Router.browserHistory.push('/edit-client?id='+action.client.client_id);

    default:
      return state;
  }

};

export default reducer;

行动调度员

export const editClient = (client_id) => dispatch => {
    axios.get(conf.getWebServiceUrl('clients/edit?client_id='+client_id), {headers: conf.getHeaders()})
      .then((response) => {
        console.log(response)
        return response.data.message;
      })
      .then((response) => {
        dispatch(editClientConst(response))
      })
      .catch((err) => {
        console.error.bind(err);
      })
  };

操作

const editClientConst = (client) => ({type: EDIT_CLIENT, client});

1 个答案:

答案 0 :(得分:1)

您可以在渲染功能中使用Redirect,并通过reducer

中的props触发状态/身份验证
import { Redirect } from 'react-router';

render() {
    if(this.state.changeClient) {
        return (
            <Redirect to="/myroute" />
        );
    }