Respond to a Single Redux Action in Multiple Reducers redux
I am using multiple reducers in my project and then combining them with combineReducers() function and have all actions in single file. when i dispatch the action, it is returning me state values to undefined. I think It can't find out because of multiple reducerse. But when i use single reducer file. It is working fine. Can anyone please tell me what the issue.It is how i am combining the reducers.
const rootReducer = combineReducers(
isMobileReducer,
imageSliderReducer
)
and now passing to store, like below:
let store = createStore(rootReducer,applyMiddleware(thunk))
and in frontend how i am accessing state
const mapStateToProps = (state) => (
images: state.images,
isMobile: state && state.isMobile
)
imageSliderReducer.js
import
FETCH_IMAGES_BEGIN,
FETCH_IMAGES_SUCCESS,
FETCH_IMAGES_FAILURE
from '../actions/actionTypes'
const initialState =
images:,
error:null
const imageSliderReducer = (state = initialState, action) =>
switch (action.type)
case FETCH_IMAGES_BEGIN:
return ...state,error:null
case FETCH_IMAGES_SUCCESS:
return ...state,images:action.payload.images
case FETCH_IMAGES_FAILURE:
return ...state,error:action.payload.error,images:
default:
return state
export default imageSliderReducer;
isMobileReducer.js
import
OPEN_MENU,
CLOSE_MENU,
SET_DEVICE_TYPE,
from '../actions/actionTypes'
const initialState =
isMenuOpen: null,
isMobile: false
const isMobileReducer = (state = initialState, action) =>
switch (action.type)
case OPEN_MENU:
return ...state, isMenuOpen: true
case CLOSE_MENU:
return ...state, isMenuOpen: false
case SET_DEVICE_TYPE:
return ...state, isMobile: action.isMobile
default:
return state
export default isMobileReducer;
actionCreator.js
import
OPEN_MENU,
CLOSE_MENU,
SET_DEVICE_TYPE,
FETCH_IMAGES_BEGIN,
FETCH_IMAGES_SUCCESS,
FETCH_IMAGES_FAILURE
from './actionTypes'
export function openMenu(isMobile)
return
type: OPEN_MENU
export function closeMenu(isMobile)
return
type: CLOSE_MENU
export function setDeviceType (isMobile)
return
type: SET_DEVICE_TYPE,
isMobile: isMobile
export function fetchImages()
return dispatch =>
dispatch(fetchImagesBegin());
return fetch("https://7344.rio.com/wp-json/customapi/homeslider")
.then(handleErrors)
.then(res => res.json())
.then(json =>
dispatch(fetchImagesSuccess(json.posts));
return json.posts;
)
.catch(error => dispatch(fetchImagesFailure(error)));
;
function handleErrors(response)
if (!response.ok)
throw Error(response.statusText);
return response;
export const fetchImagesBegin = () => (
type: FETCH_IMAGES_BEGIN
);
export const fetchImagesSuccess = images => (
type: FETCH_IMAGES_SUCCESS,
payload: images
);
export const fetchImagesFailure = error => (
type: FETCH_IMAGES_FAILURE,
payload: error
);
reactjs redux
add a comment |
I am using multiple reducers in my project and then combining them with combineReducers() function and have all actions in single file. when i dispatch the action, it is returning me state values to undefined. I think It can't find out because of multiple reducerse. But when i use single reducer file. It is working fine. Can anyone please tell me what the issue.It is how i am combining the reducers.
const rootReducer = combineReducers(
isMobileReducer,
imageSliderReducer
)
and now passing to store, like below:
let store = createStore(rootReducer,applyMiddleware(thunk))
and in frontend how i am accessing state
const mapStateToProps = (state) => (
images: state.images,
isMobile: state && state.isMobile
)
imageSliderReducer.js
import
FETCH_IMAGES_BEGIN,
FETCH_IMAGES_SUCCESS,
FETCH_IMAGES_FAILURE
from '../actions/actionTypes'
const initialState =
images:,
error:null
const imageSliderReducer = (state = initialState, action) =>
switch (action.type)
case FETCH_IMAGES_BEGIN:
return ...state,error:null
case FETCH_IMAGES_SUCCESS:
return ...state,images:action.payload.images
case FETCH_IMAGES_FAILURE:
return ...state,error:action.payload.error,images:
default:
return state
export default imageSliderReducer;
isMobileReducer.js
import
OPEN_MENU,
CLOSE_MENU,
SET_DEVICE_TYPE,
from '../actions/actionTypes'
const initialState =
isMenuOpen: null,
isMobile: false
const isMobileReducer = (state = initialState, action) =>
switch (action.type)
case OPEN_MENU:
return ...state, isMenuOpen: true
case CLOSE_MENU:
return ...state, isMenuOpen: false
case SET_DEVICE_TYPE:
return ...state, isMobile: action.isMobile
default:
return state
export default isMobileReducer;
actionCreator.js
import
OPEN_MENU,
CLOSE_MENU,
SET_DEVICE_TYPE,
FETCH_IMAGES_BEGIN,
FETCH_IMAGES_SUCCESS,
FETCH_IMAGES_FAILURE
from './actionTypes'
export function openMenu(isMobile)
return
type: OPEN_MENU
export function closeMenu(isMobile)
return
type: CLOSE_MENU
export function setDeviceType (isMobile)
return
type: SET_DEVICE_TYPE,
isMobile: isMobile
export function fetchImages()
return dispatch =>
dispatch(fetchImagesBegin());
return fetch("https://7344.rio.com/wp-json/customapi/homeslider")
.then(handleErrors)
.then(res => res.json())
.then(json =>
dispatch(fetchImagesSuccess(json.posts));
return json.posts;
)
.catch(error => dispatch(fetchImagesFailure(error)));
;
function handleErrors(response)
if (!response.ok)
throw Error(response.statusText);
return response;
export const fetchImagesBegin = () => (
type: FETCH_IMAGES_BEGIN
);
export const fetchImagesSuccess = images => (
type: FETCH_IMAGES_SUCCESS,
payload: images
);
export const fetchImagesFailure = error => (
type: FETCH_IMAGES_FAILURE,
payload: error
);
reactjs redux
can you share your reducers and actions as well ?
– Pranay Tripathi
Nov 13 '18 at 10:12
i edited the code.
– user10298495
Nov 13 '18 at 10:22
add a comment |
I am using multiple reducers in my project and then combining them with combineReducers() function and have all actions in single file. when i dispatch the action, it is returning me state values to undefined. I think It can't find out because of multiple reducerse. But when i use single reducer file. It is working fine. Can anyone please tell me what the issue.It is how i am combining the reducers.
const rootReducer = combineReducers(
isMobileReducer,
imageSliderReducer
)
and now passing to store, like below:
let store = createStore(rootReducer,applyMiddleware(thunk))
and in frontend how i am accessing state
const mapStateToProps = (state) => (
images: state.images,
isMobile: state && state.isMobile
)
imageSliderReducer.js
import
FETCH_IMAGES_BEGIN,
FETCH_IMAGES_SUCCESS,
FETCH_IMAGES_FAILURE
from '../actions/actionTypes'
const initialState =
images:,
error:null
const imageSliderReducer = (state = initialState, action) =>
switch (action.type)
case FETCH_IMAGES_BEGIN:
return ...state,error:null
case FETCH_IMAGES_SUCCESS:
return ...state,images:action.payload.images
case FETCH_IMAGES_FAILURE:
return ...state,error:action.payload.error,images:
default:
return state
export default imageSliderReducer;
isMobileReducer.js
import
OPEN_MENU,
CLOSE_MENU,
SET_DEVICE_TYPE,
from '../actions/actionTypes'
const initialState =
isMenuOpen: null,
isMobile: false
const isMobileReducer = (state = initialState, action) =>
switch (action.type)
case OPEN_MENU:
return ...state, isMenuOpen: true
case CLOSE_MENU:
return ...state, isMenuOpen: false
case SET_DEVICE_TYPE:
return ...state, isMobile: action.isMobile
default:
return state
export default isMobileReducer;
actionCreator.js
import
OPEN_MENU,
CLOSE_MENU,
SET_DEVICE_TYPE,
FETCH_IMAGES_BEGIN,
FETCH_IMAGES_SUCCESS,
FETCH_IMAGES_FAILURE
from './actionTypes'
export function openMenu(isMobile)
return
type: OPEN_MENU
export function closeMenu(isMobile)
return
type: CLOSE_MENU
export function setDeviceType (isMobile)
return
type: SET_DEVICE_TYPE,
isMobile: isMobile
export function fetchImages()
return dispatch =>
dispatch(fetchImagesBegin());
return fetch("https://7344.rio.com/wp-json/customapi/homeslider")
.then(handleErrors)
.then(res => res.json())
.then(json =>
dispatch(fetchImagesSuccess(json.posts));
return json.posts;
)
.catch(error => dispatch(fetchImagesFailure(error)));
;
function handleErrors(response)
if (!response.ok)
throw Error(response.statusText);
return response;
export const fetchImagesBegin = () => (
type: FETCH_IMAGES_BEGIN
);
export const fetchImagesSuccess = images => (
type: FETCH_IMAGES_SUCCESS,
payload: images
);
export const fetchImagesFailure = error => (
type: FETCH_IMAGES_FAILURE,
payload: error
);
reactjs redux
I am using multiple reducers in my project and then combining them with combineReducers() function and have all actions in single file. when i dispatch the action, it is returning me state values to undefined. I think It can't find out because of multiple reducerse. But when i use single reducer file. It is working fine. Can anyone please tell me what the issue.It is how i am combining the reducers.
const rootReducer = combineReducers(
isMobileReducer,
imageSliderReducer
)
and now passing to store, like below:
let store = createStore(rootReducer,applyMiddleware(thunk))
and in frontend how i am accessing state
const mapStateToProps = (state) => (
images: state.images,
isMobile: state && state.isMobile
)
imageSliderReducer.js
import
FETCH_IMAGES_BEGIN,
FETCH_IMAGES_SUCCESS,
FETCH_IMAGES_FAILURE
from '../actions/actionTypes'
const initialState =
images:,
error:null
const imageSliderReducer = (state = initialState, action) =>
switch (action.type)
case FETCH_IMAGES_BEGIN:
return ...state,error:null
case FETCH_IMAGES_SUCCESS:
return ...state,images:action.payload.images
case FETCH_IMAGES_FAILURE:
return ...state,error:action.payload.error,images:
default:
return state
export default imageSliderReducer;
isMobileReducer.js
import
OPEN_MENU,
CLOSE_MENU,
SET_DEVICE_TYPE,
from '../actions/actionTypes'
const initialState =
isMenuOpen: null,
isMobile: false
const isMobileReducer = (state = initialState, action) =>
switch (action.type)
case OPEN_MENU:
return ...state, isMenuOpen: true
case CLOSE_MENU:
return ...state, isMenuOpen: false
case SET_DEVICE_TYPE:
return ...state, isMobile: action.isMobile
default:
return state
export default isMobileReducer;
actionCreator.js
import
OPEN_MENU,
CLOSE_MENU,
SET_DEVICE_TYPE,
FETCH_IMAGES_BEGIN,
FETCH_IMAGES_SUCCESS,
FETCH_IMAGES_FAILURE
from './actionTypes'
export function openMenu(isMobile)
return
type: OPEN_MENU
export function closeMenu(isMobile)
return
type: CLOSE_MENU
export function setDeviceType (isMobile)
return
type: SET_DEVICE_TYPE,
isMobile: isMobile
export function fetchImages()
return dispatch =>
dispatch(fetchImagesBegin());
return fetch("https://7344.rio.com/wp-json/customapi/homeslider")
.then(handleErrors)
.then(res => res.json())
.then(json =>
dispatch(fetchImagesSuccess(json.posts));
return json.posts;
)
.catch(error => dispatch(fetchImagesFailure(error)));
;
function handleErrors(response)
if (!response.ok)
throw Error(response.statusText);
return response;
export const fetchImagesBegin = () => (
type: FETCH_IMAGES_BEGIN
);
export const fetchImagesSuccess = images => (
type: FETCH_IMAGES_SUCCESS,
payload: images
);
export const fetchImagesFailure = error => (
type: FETCH_IMAGES_FAILURE,
payload: error
);
reactjs redux
reactjs redux
edited Nov 13 '18 at 15:12
asked Nov 13 '18 at 10:10
user10298495
can you share your reducers and actions as well ?
– Pranay Tripathi
Nov 13 '18 at 10:12
i edited the code.
– user10298495
Nov 13 '18 at 10:22
add a comment |
can you share your reducers and actions as well ?
– Pranay Tripathi
Nov 13 '18 at 10:12
i edited the code.
– user10298495
Nov 13 '18 at 10:22
can you share your reducers and actions as well ?
– Pranay Tripathi
Nov 13 '18 at 10:12
can you share your reducers and actions as well ?
– Pranay Tripathi
Nov 13 '18 at 10:12
i edited the code.
– user10298495
Nov 13 '18 at 10:22
i edited the code.
– user10298495
Nov 13 '18 at 10:22
add a comment |
1 Answer
1
active
oldest
votes
Try using this:
const mapStateToProps = (state) => (
images: state.imageSliderReducer.images,
isMobile: state.isMobileReducer.isMobile
)
i edited the code.
– user10298495
Nov 13 '18 at 10:22
thanks, it worked
– user10298495
Nov 13 '18 at 10:45
anytime.. @Rajat
– Ashish Kirodian
Nov 13 '18 at 10:46
can you please help for one more query in redux?
– user10298495
Nov 13 '18 at 13:16
@Rajat Yeah sure
– Ashish Kirodian
Nov 13 '18 at 13:28
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278588%2frespond-to-a-single-redux-action-in-multiple-reducers-redux%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try using this:
const mapStateToProps = (state) => (
images: state.imageSliderReducer.images,
isMobile: state.isMobileReducer.isMobile
)
i edited the code.
– user10298495
Nov 13 '18 at 10:22
thanks, it worked
– user10298495
Nov 13 '18 at 10:45
anytime.. @Rajat
– Ashish Kirodian
Nov 13 '18 at 10:46
can you please help for one more query in redux?
– user10298495
Nov 13 '18 at 13:16
@Rajat Yeah sure
– Ashish Kirodian
Nov 13 '18 at 13:28
|
show 3 more comments
Try using this:
const mapStateToProps = (state) => (
images: state.imageSliderReducer.images,
isMobile: state.isMobileReducer.isMobile
)
i edited the code.
– user10298495
Nov 13 '18 at 10:22
thanks, it worked
– user10298495
Nov 13 '18 at 10:45
anytime.. @Rajat
– Ashish Kirodian
Nov 13 '18 at 10:46
can you please help for one more query in redux?
– user10298495
Nov 13 '18 at 13:16
@Rajat Yeah sure
– Ashish Kirodian
Nov 13 '18 at 13:28
|
show 3 more comments
Try using this:
const mapStateToProps = (state) => (
images: state.imageSliderReducer.images,
isMobile: state.isMobileReducer.isMobile
)
Try using this:
const mapStateToProps = (state) => (
images: state.imageSliderReducer.images,
isMobile: state.isMobileReducer.isMobile
)
edited Nov 13 '18 at 10:32
answered Nov 13 '18 at 10:20
Ashish KirodianAshish Kirodian
865
865
i edited the code.
– user10298495
Nov 13 '18 at 10:22
thanks, it worked
– user10298495
Nov 13 '18 at 10:45
anytime.. @Rajat
– Ashish Kirodian
Nov 13 '18 at 10:46
can you please help for one more query in redux?
– user10298495
Nov 13 '18 at 13:16
@Rajat Yeah sure
– Ashish Kirodian
Nov 13 '18 at 13:28
|
show 3 more comments
i edited the code.
– user10298495
Nov 13 '18 at 10:22
thanks, it worked
– user10298495
Nov 13 '18 at 10:45
anytime.. @Rajat
– Ashish Kirodian
Nov 13 '18 at 10:46
can you please help for one more query in redux?
– user10298495
Nov 13 '18 at 13:16
@Rajat Yeah sure
– Ashish Kirodian
Nov 13 '18 at 13:28
i edited the code.
– user10298495
Nov 13 '18 at 10:22
i edited the code.
– user10298495
Nov 13 '18 at 10:22
thanks, it worked
– user10298495
Nov 13 '18 at 10:45
thanks, it worked
– user10298495
Nov 13 '18 at 10:45
anytime.. @Rajat
– Ashish Kirodian
Nov 13 '18 at 10:46
anytime.. @Rajat
– Ashish Kirodian
Nov 13 '18 at 10:46
can you please help for one more query in redux?
– user10298495
Nov 13 '18 at 13:16
can you please help for one more query in redux?
– user10298495
Nov 13 '18 at 13:16
@Rajat Yeah sure
– Ashish Kirodian
Nov 13 '18 at 13:28
@Rajat Yeah sure
– Ashish Kirodian
Nov 13 '18 at 13:28
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278588%2frespond-to-a-single-redux-action-in-multiple-reducers-redux%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you share your reducers and actions as well ?
– Pranay Tripathi
Nov 13 '18 at 10:12
i edited the code.
– user10298495
Nov 13 '18 at 10:22