如何在同一导出默认值中定义的其他函数内部调用函数?

时间:2018-03-12 06:16:17

标签: react-redux

  

我的代码是: -

 function showData(data) {
        return {
            type: 'SHOWDATA',
            data,
        };
    }
 export default {
     fetchData() {
      return function (dispatch) {
      getDataApi.getData().then((response)=>dispatch(showData(response)).catch()
      };},
     updateData{
     return function (dispatch) {
     getDataApi.getData().then((response)=>if(response.isSucess)
     {dispatch(fetchData())}).catch()
    };}
  }
  

更新后的动作调用我要刷新列表,这就是我的原因   称为 dispatch(fetchData()); ,但它显示fetchData不是   如何调用相同的导出默认函数中定义的方法。

1 个答案:

答案 0 :(得分:0)

这可以帮到你吗?不是真正导出为默认值,而是命名为。

export const Actions = {
    getAll,
    add,
    update,
    view,
    search
}

function getAll(){
    return dispatch => {
        dispatch(request());
        Service.getAll()
            .then(
                response => {
                   // todo...
                },
                error => {
                    // catch error
                }
            );
    }
    function request() { return { type: Constants.LIST_REQUEST } }
    function success(data) { return { type: Constants.LIST_SUCCESS, data } }
    function failure(error) { return { type: Constants.LIST_FAILURE, error } }
}

function add(data){
    return dispatch => {
        dispatch(request());
        Service.add(data)
            .then(
                response => {
                    if(response.status === 'fail'){
                        // do something
                    }else{
                        dispatch(success(response));
                        dispatch(getAll());
                    }
                },
                error => {
                    // do something
                }
            );
    }
    function request() { return { type: Constants.ADD_REQUEST } }
    function success(data) { return { type: Constants.ADD_SUCCESS, data } }
    function failure(error) { return { type: Constants.ADD_FAILURE, error } }
}
相关问题