React component doesn't connect to the store
I have been using this to create the following.
import React from 'react';
import ReactDOM from 'react-dom';
import {connect} 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';
const WarningModal = ({withCleanup, message, isWarning}) => {
const [
title,
text,
leave,
cancel
] = message.split('|');
console.log(isWarning)
const handleOnClick = () => {
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,
isWarning: PropTypes.bool.isRequired
};
const mapStateToProps = state => {
return {
isWarning: state.app.isWarning
}
};
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(
<WarningModal
message={message}
withCleanup={withCleanup}
/>,
modal
);
};
I need to dispatch an action to update a state property ('isWarning') which controls the visibility of 'WarningModal', however this doesn't seem to connect my component to the store:
connect(mapStateToProps, {
setWarning
})(WarningModal);
ConfigureStore.js
...
return createStore(
combineReducers(reducers),
composeEnhancers(
applyMiddleware(...middlewares)
)
);
...
javascript reactjs redux react-redux
add a comment |
I have been using this to create the following.
import React from 'react';
import ReactDOM from 'react-dom';
import {connect} 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';
const WarningModal = ({withCleanup, message, isWarning}) => {
const [
title,
text,
leave,
cancel
] = message.split('|');
console.log(isWarning)
const handleOnClick = () => {
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,
isWarning: PropTypes.bool.isRequired
};
const mapStateToProps = state => {
return {
isWarning: state.app.isWarning
}
};
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(
<WarningModal
message={message}
withCleanup={withCleanup}
/>,
modal
);
};
I need to dispatch an action to update a state property ('isWarning') which controls the visibility of 'WarningModal', however this doesn't seem to connect my component to the store:
connect(mapStateToProps, {
setWarning
})(WarningModal);
ConfigureStore.js
...
return createStore(
combineReducers(reducers),
composeEnhancers(
applyMiddleware(...middlewares)
)
);
...
javascript reactjs redux react-redux
Are you using combineReducers? Can you share your reducer/rootReducer structure?
– Alexander Staroselsky
Nov 21 '18 at 17:03
please check this , stackoverflow.com/questions/50251827/…
– DILEEP THOMAS
Nov 21 '18 at 17:04
@AlexanderStaroselsky yes I am
– Alex
Nov 21 '18 at 17:08
I'm trying to understand if yourmapStateToProps
configured correctly. The question about thecombineReducers
was to get information regarding the keys/namespaces of your state/store. Can youconsole.log(state)
insidemapStateToProps
and share the output please?
– Alexander Staroselsky
Nov 21 '18 at 17:14
@AlexanderStaroselsky it's undefined, the relative action and reducers are called.
– Alex
Nov 21 '18 at 17:17
add a comment |
I have been using this to create the following.
import React from 'react';
import ReactDOM from 'react-dom';
import {connect} 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';
const WarningModal = ({withCleanup, message, isWarning}) => {
const [
title,
text,
leave,
cancel
] = message.split('|');
console.log(isWarning)
const handleOnClick = () => {
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,
isWarning: PropTypes.bool.isRequired
};
const mapStateToProps = state => {
return {
isWarning: state.app.isWarning
}
};
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(
<WarningModal
message={message}
withCleanup={withCleanup}
/>,
modal
);
};
I need to dispatch an action to update a state property ('isWarning') which controls the visibility of 'WarningModal', however this doesn't seem to connect my component to the store:
connect(mapStateToProps, {
setWarning
})(WarningModal);
ConfigureStore.js
...
return createStore(
combineReducers(reducers),
composeEnhancers(
applyMiddleware(...middlewares)
)
);
...
javascript reactjs redux react-redux
I have been using this to create the following.
import React from 'react';
import ReactDOM from 'react-dom';
import {connect} 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';
const WarningModal = ({withCleanup, message, isWarning}) => {
const [
title,
text,
leave,
cancel
] = message.split('|');
console.log(isWarning)
const handleOnClick = () => {
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,
isWarning: PropTypes.bool.isRequired
};
const mapStateToProps = state => {
return {
isWarning: state.app.isWarning
}
};
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(
<WarningModal
message={message}
withCleanup={withCleanup}
/>,
modal
);
};
I need to dispatch an action to update a state property ('isWarning') which controls the visibility of 'WarningModal', however this doesn't seem to connect my component to the store:
connect(mapStateToProps, {
setWarning
})(WarningModal);
ConfigureStore.js
...
return createStore(
combineReducers(reducers),
composeEnhancers(
applyMiddleware(...middlewares)
)
);
...
javascript reactjs redux react-redux
javascript reactjs redux react-redux
edited Nov 21 '18 at 17:04
Alex
asked Nov 21 '18 at 16:56
AlexAlex
1,78283980
1,78283980
Are you using combineReducers? Can you share your reducer/rootReducer structure?
– Alexander Staroselsky
Nov 21 '18 at 17:03
please check this , stackoverflow.com/questions/50251827/…
– DILEEP THOMAS
Nov 21 '18 at 17:04
@AlexanderStaroselsky yes I am
– Alex
Nov 21 '18 at 17:08
I'm trying to understand if yourmapStateToProps
configured correctly. The question about thecombineReducers
was to get information regarding the keys/namespaces of your state/store. Can youconsole.log(state)
insidemapStateToProps
and share the output please?
– Alexander Staroselsky
Nov 21 '18 at 17:14
@AlexanderStaroselsky it's undefined, the relative action and reducers are called.
– Alex
Nov 21 '18 at 17:17
add a comment |
Are you using combineReducers? Can you share your reducer/rootReducer structure?
– Alexander Staroselsky
Nov 21 '18 at 17:03
please check this , stackoverflow.com/questions/50251827/…
– DILEEP THOMAS
Nov 21 '18 at 17:04
@AlexanderStaroselsky yes I am
– Alex
Nov 21 '18 at 17:08
I'm trying to understand if yourmapStateToProps
configured correctly. The question about thecombineReducers
was to get information regarding the keys/namespaces of your state/store. Can youconsole.log(state)
insidemapStateToProps
and share the output please?
– Alexander Staroselsky
Nov 21 '18 at 17:14
@AlexanderStaroselsky it's undefined, the relative action and reducers are called.
– Alex
Nov 21 '18 at 17:17
Are you using combineReducers? Can you share your reducer/rootReducer structure?
– Alexander Staroselsky
Nov 21 '18 at 17:03
Are you using combineReducers? Can you share your reducer/rootReducer structure?
– Alexander Staroselsky
Nov 21 '18 at 17:03
please check this , stackoverflow.com/questions/50251827/…
– DILEEP THOMAS
Nov 21 '18 at 17:04
please check this , stackoverflow.com/questions/50251827/…
– DILEEP THOMAS
Nov 21 '18 at 17:04
@AlexanderStaroselsky yes I am
– Alex
Nov 21 '18 at 17:08
@AlexanderStaroselsky yes I am
– Alex
Nov 21 '18 at 17:08
I'm trying to understand if your
mapStateToProps
configured correctly. The question about the combineReducers
was to get information regarding the keys/namespaces of your state/store. Can you console.log(state)
inside mapStateToProps
and share the output please?– Alexander Staroselsky
Nov 21 '18 at 17:14
I'm trying to understand if your
mapStateToProps
configured correctly. The question about the combineReducers
was to get information regarding the keys/namespaces of your state/store. Can you console.log(state)
inside mapStateToProps
and share the output please?– Alexander Staroselsky
Nov 21 '18 at 17:14
@AlexanderStaroselsky it's undefined, the relative action and reducers are called.
– Alex
Nov 21 '18 at 17:17
@AlexanderStaroselsky it's undefined, the relative action and reducers are called.
– Alex
Nov 21 '18 at 17:17
add a comment |
2 Answers
2
active
oldest
votes
The call to connect()
returns a wrapped React component constructor. This code is not using the return value of connect
at all, so it's thrown away.
The smallest change that should fix this would be to save the return value from connect
in a new variable, and use that in place of WarningModal
in the exported component constructor's render function:
const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);
...
ReactDOM.render(
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>,
modal
);
add a comment |
There should be
export default connect(mapStateToProps, {
setWarning
})(WarningModal);
This way you cen import redux connected component for rendering (in separate file) - in your version render takes original (not connected) component for mount,
It can be done in one file but it isn't recommended.
add a comment |
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%2f53417050%2freact-component-doesnt-connect-to-the-store%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The call to connect()
returns a wrapped React component constructor. This code is not using the return value of connect
at all, so it's thrown away.
The smallest change that should fix this would be to save the return value from connect
in a new variable, and use that in place of WarningModal
in the exported component constructor's render function:
const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);
...
ReactDOM.render(
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>,
modal
);
add a comment |
The call to connect()
returns a wrapped React component constructor. This code is not using the return value of connect
at all, so it's thrown away.
The smallest change that should fix this would be to save the return value from connect
in a new variable, and use that in place of WarningModal
in the exported component constructor's render function:
const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);
...
ReactDOM.render(
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>,
modal
);
add a comment |
The call to connect()
returns a wrapped React component constructor. This code is not using the return value of connect
at all, so it's thrown away.
The smallest change that should fix this would be to save the return value from connect
in a new variable, and use that in place of WarningModal
in the exported component constructor's render function:
const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);
...
ReactDOM.render(
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>,
modal
);
The call to connect()
returns a wrapped React component constructor. This code is not using the return value of connect
at all, so it's thrown away.
The smallest change that should fix this would be to save the return value from connect
in a new variable, and use that in place of WarningModal
in the exported component constructor's render function:
const WarningModalContainer = connect(mapStateToProps, {
setWarning
})(WarningModal);
...
ReactDOM.render(
<WarningModalContainer
message={message}
withCleanup={withCleanup}
/>,
modal
);
answered Nov 22 '18 at 0:50
stonestone
4,68424059
4,68424059
add a comment |
add a comment |
There should be
export default connect(mapStateToProps, {
setWarning
})(WarningModal);
This way you cen import redux connected component for rendering (in separate file) - in your version render takes original (not connected) component for mount,
It can be done in one file but it isn't recommended.
add a comment |
There should be
export default connect(mapStateToProps, {
setWarning
})(WarningModal);
This way you cen import redux connected component for rendering (in separate file) - in your version render takes original (not connected) component for mount,
It can be done in one file but it isn't recommended.
add a comment |
There should be
export default connect(mapStateToProps, {
setWarning
})(WarningModal);
This way you cen import redux connected component for rendering (in separate file) - in your version render takes original (not connected) component for mount,
It can be done in one file but it isn't recommended.
There should be
export default connect(mapStateToProps, {
setWarning
})(WarningModal);
This way you cen import redux connected component for rendering (in separate file) - in your version render takes original (not connected) component for mount,
It can be done in one file but it isn't recommended.
answered Nov 21 '18 at 17:17
xadmxadm
1,641258
1,641258
add a comment |
add a comment |
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%2f53417050%2freact-component-doesnt-connect-to-the-store%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
Are you using combineReducers? Can you share your reducer/rootReducer structure?
– Alexander Staroselsky
Nov 21 '18 at 17:03
please check this , stackoverflow.com/questions/50251827/…
– DILEEP THOMAS
Nov 21 '18 at 17:04
@AlexanderStaroselsky yes I am
– Alex
Nov 21 '18 at 17:08
I'm trying to understand if your
mapStateToProps
configured correctly. The question about thecombineReducers
was to get information regarding the keys/namespaces of your state/store. Can youconsole.log(state)
insidemapStateToProps
and share the output please?– Alexander Staroselsky
Nov 21 '18 at 17:14
@AlexanderStaroselsky it's undefined, the relative action and reducers are called.
– Alex
Nov 21 '18 at 17:17