2017-07-26 36 views
0

什麼是推薦的方式:調度和重定向

  1. 分派事件使用react-router
  2. 成分中含有
  3. 重定向到其他頁面的props數據

繼使用redux-form成功完成異步表單提交後?

+0

我現有的解決方案似乎都遇到以下問題:https://github.com/erikras/redux-form/issues/3108 – Anvar

回答

0

我設置了一個回調函數,當調度已交付的功能被稱爲:

組件

onSubmit() { 
    const { userEmail, userPassword } = this.props; 
     this.props.loginUser({ userEmail, userPassword },() => { 
     this.props.history.push('/dashboard'); 
    }); 
} 

行動

export const loginUser = ({ userEmail, userPassword }, callback) => { 
    return(dispatch) => { 
    dispatch({ type: LOGIN_USER }); 
    auth.signInWithEmailAndPassword(userEmail, userPassword) 
    .then(
     user => { 
     loginUserSuccess(dispatch, user); 
     }, 
     error => {  
     loginUserFail(dispatch, error.message); 
     } 
    ) 
     redirectFunction(callback); 
    } 
} 

// Helper functions 
const loginUserSuccess = (dispatch, user) => { 
    dispatch({ 
     type: LOGIN_USER_SUCCESS, 
     payload: user 
    }); 
}; 

const loginUserFail = (dispatch, error) => { 
    dispatch({ 
     type: LOGIN_USER_FAIL, 
     error: error 
    }); 
}; 

減速

case LOGIN_USER: 
     return { ...state, loading: true, error: '' }; 
    case LOGIN_USER_SUCCESS: 
     console.log(action.payload); 
     return { ...state, ...INITIAL_STATE, user: action.payload, loggedIn: true }; 
    case LOGIN_USER_FAIL: 
     return { ...state, error: action.error }; 
0

管理人調度和redux-form回調onSubmitSuccess/onSubmitFailure內部重定向來解決它。