是否有任何方法可以散布动作并将其中一些绑定到特定的reducer?

时间:2019-07-20 11:57:28

标签: redux

我是Redux的新手,如果我的问题微不足道,我会举报,但我找不到答案。

我有几个reducer与CombineReducers()和一个大型ActionCreator.js结合在一起,可以执行所有操作。

是否有某种好的方法可以将此ActionCreator拆分为多个文件,并将特定的Action文件绑定到特定的reducer,以提高生产率并简化代码读取?

//reducer.js:
    
    import { combineReducers } from 'redux';
    import { authReducer} from './authReducer';
    import { modalHandler } from './modalHandler';
    import { rootReducer } from './rootReducer';
    import { errorHandler } from './errorHandler';
    
    const reducer = combineReducers({ authReducer, modalHandler, rootReducer, errorHandler });
    
    export default reducer;
    
    
    //ActionCreator.js:
    
    export const newMessage = (id, mess)=> {
      return dispatch => {
        return axios.post('/api/channel/newMessage', {id, mess})
          .then(res => {
            dispatch(getChannelData());
          })
          .catch(err => {
            dispatch(requestError(err));
          });
      };
    };
    
    export const channelLogoUpload = formData => {
      return dispatch => {
        return axios.post('/api/uploads/newChannel/logo', formData)
          .then(res => {
            console.log(res.data.url)
            dispatch(newLogo(res.data.url))
          })
          .catch(err => dispatch(requestError(err)));
      }
    }
    
    export const userRegistration = user => {
      console.log(user);
      return dispatch => {
        return axios.post('/api/signup', { user })
          .then(res => {
            if (res.status === 200) {
              dispatch(newUser(true));
            }
          })
          .catch(err => {
            if (err) {
              dispatch(registrationError(err.response.data.message));
            }
          })
      }
    }
    
    export const imageIncrease = (value, title) => ({
      type: IMAGE_INCREASE,
      value,
      title
    });
    
    export const logOut = () => ({
      type: LOG_OUT
    });
    
    //...etc
    
    
    // I want something like this:
     
    const combineReducers({
       authActions: authReducer,
       modalActions: modalHandler,
      //...etc
    })

1 个答案:

答案 0 :(得分:0)

我不知道什么是标准做法,但是在我之前的项目中,我们曾经将所有动作创建者与其化简器一起写入同一文件中,然后我们将所有这些化简器组合在一起,这样您就可以更改动作创建者对特定的还原剂有影响。