Action doesn't update the store





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















|I have the following component based on this:



**WarningModal.js**

import React from 'react';
import ReactDOM from 'react-dom';
import {connect, Provider} from 'react-redux';
import PropTypes from 'prop-types';

import {Alert, No} from './pure/Icons/Icons';
import Button from './pure/Button/Button';
import Modal from './pure/Modal/Modal';

import {setWarning} from '../actions/app/appActions';

import configureStore from '../store/configureStore';

const store = configureStore();

export const WarningModal = (props) => {
const {message, withCleanup} = props;
const [
title,
text,
leave,
cancel
] = message.split('|');

const handleOnClick = () => {
props.setWarning(false);
withCleanup(true);
}

return(
<Modal>
<header>{title}</header>
<p>{text}</p>
<Alert />
<div className="modal__buttons-wrapper modal__buttons-wrapper--center">
<button
onClick={() => withCleanup(false)}
className="button modal__close-button button--icon button--icon-only button--text-link"
>
<No />
</button>
<Button id="leave-warning-button" className="button--transparent-bg" onClick={() => handleOnClick()}>{leave}</Button>
<Button id="cancel-warning-button" onClick={() => withCleanup(false)}>{cancel}</Button>
</div>
</Modal>
);
}

WarningModal.propTypes = {
withCleanup: PropTypes.func.isRequired,
message: PropTypes.string.isRequired,
setWarning: PropTypes.func.isRequired
};

const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);




export default (message, callback) => {
const modal = document.createElement('div');
document.body.appendChild(modal);

const withCleanup = (answer) => {
ReactDOM.unmountComponentAtNode(modal);
document.body.removeChild(modal);
callback(answer);
};

ReactDOM.render(
<Provider store={store}>
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>
</Provider>,
modal
);
};


the issue I have is that 'setWarning' doesn't update the state, it does get called as I have a debugger inside the action and the reducer but the actual property doesn't not change to 'false' when:



props.setWarning(false);


gets called.



I use the following to trigger the custom modal:



    const togglePromptCondition = 
location.hash === '#access-templates' || location.hash === '#security-groups'
? promptCondition
: isFormDirty || isWarning;

<Prompt message={promptMessage} when={togglePromptCondition} />


To test this even further I have added 2 buttons in the application to toggle 'isWarning' (the state property I am talking about) and it works as expected.



I think that although WarningModal is connected in actual fact it isn't.



REDUCER



...
case SET_WARNING:
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...


ACTION



...
export const setWarning = status => {
console.log('action called')
return {
type: SET_WARNING,
payload: status
}
};
...


UPDATE



After having to incorporates the following:



const mapStateToProps = state => {
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};


I am now getting:



enter image description here



Maybe this could help?










share|improve this question




















  • 1





    Can you show your reducer? The issue is likely there.

    – Colin
    Nov 22 '18 at 12:19











  • @Colin I have updated the code with the reducer and action. Thanks

    – Alex
    Nov 22 '18 at 13:45













  • Do you have const SET_WARNING = 'SET_WARNING' defined?

    – varit05
    Nov 22 '18 at 13:49













  • console.log('action called'), what's the status if you log it there?

    – Colin
    Nov 22 '18 at 13:55











  • @Colin that doesn't get called once the modal is loaded

    – Alex
    Nov 22 '18 at 14:15


















0















|I have the following component based on this:



**WarningModal.js**

import React from 'react';
import ReactDOM from 'react-dom';
import {connect, Provider} from 'react-redux';
import PropTypes from 'prop-types';

import {Alert, No} from './pure/Icons/Icons';
import Button from './pure/Button/Button';
import Modal from './pure/Modal/Modal';

import {setWarning} from '../actions/app/appActions';

import configureStore from '../store/configureStore';

const store = configureStore();

export const WarningModal = (props) => {
const {message, withCleanup} = props;
const [
title,
text,
leave,
cancel
] = message.split('|');

const handleOnClick = () => {
props.setWarning(false);
withCleanup(true);
}

return(
<Modal>
<header>{title}</header>
<p>{text}</p>
<Alert />
<div className="modal__buttons-wrapper modal__buttons-wrapper--center">
<button
onClick={() => withCleanup(false)}
className="button modal__close-button button--icon button--icon-only button--text-link"
>
<No />
</button>
<Button id="leave-warning-button" className="button--transparent-bg" onClick={() => handleOnClick()}>{leave}</Button>
<Button id="cancel-warning-button" onClick={() => withCleanup(false)}>{cancel}</Button>
</div>
</Modal>
);
}

WarningModal.propTypes = {
withCleanup: PropTypes.func.isRequired,
message: PropTypes.string.isRequired,
setWarning: PropTypes.func.isRequired
};

const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);




export default (message, callback) => {
const modal = document.createElement('div');
document.body.appendChild(modal);

const withCleanup = (answer) => {
ReactDOM.unmountComponentAtNode(modal);
document.body.removeChild(modal);
callback(answer);
};

ReactDOM.render(
<Provider store={store}>
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>
</Provider>,
modal
);
};


the issue I have is that 'setWarning' doesn't update the state, it does get called as I have a debugger inside the action and the reducer but the actual property doesn't not change to 'false' when:



props.setWarning(false);


gets called.



I use the following to trigger the custom modal:



    const togglePromptCondition = 
location.hash === '#access-templates' || location.hash === '#security-groups'
? promptCondition
: isFormDirty || isWarning;

<Prompt message={promptMessage} when={togglePromptCondition} />


To test this even further I have added 2 buttons in the application to toggle 'isWarning' (the state property I am talking about) and it works as expected.



I think that although WarningModal is connected in actual fact it isn't.



REDUCER



...
case SET_WARNING:
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...


ACTION



...
export const setWarning = status => {
console.log('action called')
return {
type: SET_WARNING,
payload: status
}
};
...


UPDATE



After having to incorporates the following:



const mapStateToProps = state => {
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};


I am now getting:



enter image description here



Maybe this could help?










share|improve this question




















  • 1





    Can you show your reducer? The issue is likely there.

    – Colin
    Nov 22 '18 at 12:19











  • @Colin I have updated the code with the reducer and action. Thanks

    – Alex
    Nov 22 '18 at 13:45













  • Do you have const SET_WARNING = 'SET_WARNING' defined?

    – varit05
    Nov 22 '18 at 13:49













  • console.log('action called'), what's the status if you log it there?

    – Colin
    Nov 22 '18 at 13:55











  • @Colin that doesn't get called once the modal is loaded

    – Alex
    Nov 22 '18 at 14:15














0












0








0








|I have the following component based on this:



**WarningModal.js**

import React from 'react';
import ReactDOM from 'react-dom';
import {connect, Provider} from 'react-redux';
import PropTypes from 'prop-types';

import {Alert, No} from './pure/Icons/Icons';
import Button from './pure/Button/Button';
import Modal from './pure/Modal/Modal';

import {setWarning} from '../actions/app/appActions';

import configureStore from '../store/configureStore';

const store = configureStore();

export const WarningModal = (props) => {
const {message, withCleanup} = props;
const [
title,
text,
leave,
cancel
] = message.split('|');

const handleOnClick = () => {
props.setWarning(false);
withCleanup(true);
}

return(
<Modal>
<header>{title}</header>
<p>{text}</p>
<Alert />
<div className="modal__buttons-wrapper modal__buttons-wrapper--center">
<button
onClick={() => withCleanup(false)}
className="button modal__close-button button--icon button--icon-only button--text-link"
>
<No />
</button>
<Button id="leave-warning-button" className="button--transparent-bg" onClick={() => handleOnClick()}>{leave}</Button>
<Button id="cancel-warning-button" onClick={() => withCleanup(false)}>{cancel}</Button>
</div>
</Modal>
);
}

WarningModal.propTypes = {
withCleanup: PropTypes.func.isRequired,
message: PropTypes.string.isRequired,
setWarning: PropTypes.func.isRequired
};

const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);




export default (message, callback) => {
const modal = document.createElement('div');
document.body.appendChild(modal);

const withCleanup = (answer) => {
ReactDOM.unmountComponentAtNode(modal);
document.body.removeChild(modal);
callback(answer);
};

ReactDOM.render(
<Provider store={store}>
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>
</Provider>,
modal
);
};


the issue I have is that 'setWarning' doesn't update the state, it does get called as I have a debugger inside the action and the reducer but the actual property doesn't not change to 'false' when:



props.setWarning(false);


gets called.



I use the following to trigger the custom modal:



    const togglePromptCondition = 
location.hash === '#access-templates' || location.hash === '#security-groups'
? promptCondition
: isFormDirty || isWarning;

<Prompt message={promptMessage} when={togglePromptCondition} />


To test this even further I have added 2 buttons in the application to toggle 'isWarning' (the state property I am talking about) and it works as expected.



I think that although WarningModal is connected in actual fact it isn't.



REDUCER



...
case SET_WARNING:
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...


ACTION



...
export const setWarning = status => {
console.log('action called')
return {
type: SET_WARNING,
payload: status
}
};
...


UPDATE



After having to incorporates the following:



const mapStateToProps = state => {
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};


I am now getting:



enter image description here



Maybe this could help?










share|improve this question
















|I have the following component based on this:



**WarningModal.js**

import React from 'react';
import ReactDOM from 'react-dom';
import {connect, Provider} from 'react-redux';
import PropTypes from 'prop-types';

import {Alert, No} from './pure/Icons/Icons';
import Button from './pure/Button/Button';
import Modal from './pure/Modal/Modal';

import {setWarning} from '../actions/app/appActions';

import configureStore from '../store/configureStore';

const store = configureStore();

export const WarningModal = (props) => {
const {message, withCleanup} = props;
const [
title,
text,
leave,
cancel
] = message.split('|');

const handleOnClick = () => {
props.setWarning(false);
withCleanup(true);
}

return(
<Modal>
<header>{title}</header>
<p>{text}</p>
<Alert />
<div className="modal__buttons-wrapper modal__buttons-wrapper--center">
<button
onClick={() => withCleanup(false)}
className="button modal__close-button button--icon button--icon-only button--text-link"
>
<No />
</button>
<Button id="leave-warning-button" className="button--transparent-bg" onClick={() => handleOnClick()}>{leave}</Button>
<Button id="cancel-warning-button" onClick={() => withCleanup(false)}>{cancel}</Button>
</div>
</Modal>
);
}

WarningModal.propTypes = {
withCleanup: PropTypes.func.isRequired,
message: PropTypes.string.isRequired,
setWarning: PropTypes.func.isRequired
};

const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);




export default (message, callback) => {
const modal = document.createElement('div');
document.body.appendChild(modal);

const withCleanup = (answer) => {
ReactDOM.unmountComponentAtNode(modal);
document.body.removeChild(modal);
callback(answer);
};

ReactDOM.render(
<Provider store={store}>
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>
</Provider>,
modal
);
};


the issue I have is that 'setWarning' doesn't update the state, it does get called as I have a debugger inside the action and the reducer but the actual property doesn't not change to 'false' when:



props.setWarning(false);


gets called.



I use the following to trigger the custom modal:



    const togglePromptCondition = 
location.hash === '#access-templates' || location.hash === '#security-groups'
? promptCondition
: isFormDirty || isWarning;

<Prompt message={promptMessage} when={togglePromptCondition} />


To test this even further I have added 2 buttons in the application to toggle 'isWarning' (the state property I am talking about) and it works as expected.



I think that although WarningModal is connected in actual fact it isn't.



REDUCER



...
case SET_WARNING:
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...


ACTION



...
export const setWarning = status => {
console.log('action called')
return {
type: SET_WARNING,
payload: status
}
};
...


UPDATE



After having to incorporates the following:



const mapStateToProps = state => {
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};


I am now getting:



enter image description here



Maybe this could help?







javascript reactjs redux react-router






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:53







Alex

















asked Nov 22 '18 at 12:10









AlexAlex

1,78784081




1,78784081








  • 1





    Can you show your reducer? The issue is likely there.

    – Colin
    Nov 22 '18 at 12:19











  • @Colin I have updated the code with the reducer and action. Thanks

    – Alex
    Nov 22 '18 at 13:45













  • Do you have const SET_WARNING = 'SET_WARNING' defined?

    – varit05
    Nov 22 '18 at 13:49













  • console.log('action called'), what's the status if you log it there?

    – Colin
    Nov 22 '18 at 13:55











  • @Colin that doesn't get called once the modal is loaded

    – Alex
    Nov 22 '18 at 14:15














  • 1





    Can you show your reducer? The issue is likely there.

    – Colin
    Nov 22 '18 at 12:19











  • @Colin I have updated the code with the reducer and action. Thanks

    – Alex
    Nov 22 '18 at 13:45













  • Do you have const SET_WARNING = 'SET_WARNING' defined?

    – varit05
    Nov 22 '18 at 13:49













  • console.log('action called'), what's the status if you log it there?

    – Colin
    Nov 22 '18 at 13:55











  • @Colin that doesn't get called once the modal is loaded

    – Alex
    Nov 22 '18 at 14:15








1




1





Can you show your reducer? The issue is likely there.

– Colin
Nov 22 '18 at 12:19





Can you show your reducer? The issue is likely there.

– Colin
Nov 22 '18 at 12:19













@Colin I have updated the code with the reducer and action. Thanks

– Alex
Nov 22 '18 at 13:45







@Colin I have updated the code with the reducer and action. Thanks

– Alex
Nov 22 '18 at 13:45















Do you have const SET_WARNING = 'SET_WARNING' defined?

– varit05
Nov 22 '18 at 13:49







Do you have const SET_WARNING = 'SET_WARNING' defined?

– varit05
Nov 22 '18 at 13:49















console.log('action called'), what's the status if you log it there?

– Colin
Nov 22 '18 at 13:55





console.log('action called'), what's the status if you log it there?

– Colin
Nov 22 '18 at 13:55













@Colin that doesn't get called once the modal is loaded

– Alex
Nov 22 '18 at 14:15





@Colin that doesn't get called once the modal is loaded

– Alex
Nov 22 '18 at 14:15












1 Answer
1






active

oldest

votes


















0














You have to dispatch the actions in the action creator and the type of the action to dispatch should be always string.



Try this



const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
console.log(dispatch)
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};

const WarningModalContainer = connect(mapStateToProps, mapDispatchToProps)(WarningModal);


REDUCER



...
case 'SET_WARNING':
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...





share|improve this answer
























  • Hi, that's great but now I am getting an error when 'handleOnClick' is called.

    – Alex
    Nov 22 '18 at 15:13











  • Why would this make a difference if SET_WARNING = 'SET_WARNING'?

    – Colin
    Nov 22 '18 at 15:19











  • That will help too @Colin

    – Matheswaaran
    Nov 23 '18 at 5:03












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430721%2faction-doesnt-update-the-store%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









0














You have to dispatch the actions in the action creator and the type of the action to dispatch should be always string.



Try this



const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
console.log(dispatch)
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};

const WarningModalContainer = connect(mapStateToProps, mapDispatchToProps)(WarningModal);


REDUCER



...
case 'SET_WARNING':
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...





share|improve this answer
























  • Hi, that's great but now I am getting an error when 'handleOnClick' is called.

    – Alex
    Nov 22 '18 at 15:13











  • Why would this make a difference if SET_WARNING = 'SET_WARNING'?

    – Colin
    Nov 22 '18 at 15:19











  • That will help too @Colin

    – Matheswaaran
    Nov 23 '18 at 5:03
















0














You have to dispatch the actions in the action creator and the type of the action to dispatch should be always string.



Try this



const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
console.log(dispatch)
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};

const WarningModalContainer = connect(mapStateToProps, mapDispatchToProps)(WarningModal);


REDUCER



...
case 'SET_WARNING':
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...





share|improve this answer
























  • Hi, that's great but now I am getting an error when 'handleOnClick' is called.

    – Alex
    Nov 22 '18 at 15:13











  • Why would this make a difference if SET_WARNING = 'SET_WARNING'?

    – Colin
    Nov 22 '18 at 15:19











  • That will help too @Colin

    – Matheswaaran
    Nov 23 '18 at 5:03














0












0








0







You have to dispatch the actions in the action creator and the type of the action to dispatch should be always string.



Try this



const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
console.log(dispatch)
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};

const WarningModalContainer = connect(mapStateToProps, mapDispatchToProps)(WarningModal);


REDUCER



...
case 'SET_WARNING':
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...





share|improve this answer













You have to dispatch the actions in the action creator and the type of the action to dispatch should be always string.



Try this



const mapStateToProps = state => {
console.log(state)
return {
isWarning: state.app.isWarning
}
};

const mapDispatchToProps = dispatch => {
console.log(dispatch)
return {
setWarning: (status) => dispatch({ type: 'SET_WARNING', payload: status })
}
};

const WarningModalContainer = connect(mapStateToProps, mapDispatchToProps)(WarningModal);


REDUCER



...
case 'SET_WARNING':
console.log('reducer called: ', action)
return {
...state,
isWarning: action.payload
};
...






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 14:24









MatheswaaranMatheswaaran

727




727













  • Hi, that's great but now I am getting an error when 'handleOnClick' is called.

    – Alex
    Nov 22 '18 at 15:13











  • Why would this make a difference if SET_WARNING = 'SET_WARNING'?

    – Colin
    Nov 22 '18 at 15:19











  • That will help too @Colin

    – Matheswaaran
    Nov 23 '18 at 5:03



















  • Hi, that's great but now I am getting an error when 'handleOnClick' is called.

    – Alex
    Nov 22 '18 at 15:13











  • Why would this make a difference if SET_WARNING = 'SET_WARNING'?

    – Colin
    Nov 22 '18 at 15:19











  • That will help too @Colin

    – Matheswaaran
    Nov 23 '18 at 5:03

















Hi, that's great but now I am getting an error when 'handleOnClick' is called.

– Alex
Nov 22 '18 at 15:13





Hi, that's great but now I am getting an error when 'handleOnClick' is called.

– Alex
Nov 22 '18 at 15:13













Why would this make a difference if SET_WARNING = 'SET_WARNING'?

– Colin
Nov 22 '18 at 15:19





Why would this make a difference if SET_WARNING = 'SET_WARNING'?

– Colin
Nov 22 '18 at 15:19













That will help too @Colin

– Matheswaaran
Nov 23 '18 at 5:03





That will help too @Colin

– Matheswaaran
Nov 23 '18 at 5:03




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430721%2faction-doesnt-update-the-store%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?