J
J
justadumb2019-07-20 18:04:07
JavaScript
justadumb, 2019-07-20 18:04:07

Is there a way to separate actions and bind them to specific reducers?

I'm trying to deal with Redax, I apologize if my question is banal.
I have several reducers put together with combineReducers() and one big ActionCreator.js with all the actions. Is there any good practice to separate actions and bind them to specific reducers? (in order to improve performance and improve readability)

//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
    
    
    // хотелось бы получить что-то подобное:
     
    const combineReducers({
       authActions: authReducer,
       modalActions: modalHandler,
      //...etc
    })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-07-21
@justadumb

The standard practice is to create an actions directory and store the actions in files with the same name as the reducers. Example:
Some nonsense. Mixed actions and combineReducers, it is not clear what effect you are trying to achieve.
I advise you to use the redux-act library. Reduces boilerplate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question