Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question