Prop is not updating as expected when value is set in a reducer
I encountered this problem when I was testing my newly created action and reducer. Prop is not being updated even though I'm setting it to a fixed value in my reducer. Here are the codes that I did in my testing.
Component:
class <ComponentName> extends Component {
componentDidMount() {
login()
}
render() {
if(this.props.isLogged)
return (
<App/>
);
else
return (
<ErrorScreen/>
);
}
}
function mapStateToProps(state) {
return {
isLogged:state.auth.isLogged
}
}
const mapDispatchToProps = (dispatch) => {
return {
login: () => dispatch(login())
};
};
export default connect(mapStateToProps,mapDispatchToProps)(<ComponentName>)
Action:
export function login() {
return {
type:"TEST"
}
}
Reducer:
const initState = {
isLogged: false,
}
export default (state=initState, action) => {
switch(action.type) {
case "TEST":
return {
...state,
isLogged: true
}
break;
default:
return state
}
}
Combine Reducer:
import {combineReducers} from 'redux'
import AuthenticationReducer from './authenticationReducer'
export default combineReducers({
auth: AuthenticationReducer
})
Provider:
import React, {Component} from "react";
import <ComponentName> from './app/screens/<ComponentName>'
import store from './app/store'
import {Provider} from 'react-redux'
export default () =>
<Provider store={store}>
<<ComponentName>/>
</Provider>;
Been trying to debug this for few hours now. I am completely clueless why this is happening. Maybe I implemented it wrongly? I don't know anymore. If there are some files I forgot to include, please inform me. Thanks and have a nice day!
javascript react-native redux
|
show 6 more comments
I encountered this problem when I was testing my newly created action and reducer. Prop is not being updated even though I'm setting it to a fixed value in my reducer. Here are the codes that I did in my testing.
Component:
class <ComponentName> extends Component {
componentDidMount() {
login()
}
render() {
if(this.props.isLogged)
return (
<App/>
);
else
return (
<ErrorScreen/>
);
}
}
function mapStateToProps(state) {
return {
isLogged:state.auth.isLogged
}
}
const mapDispatchToProps = (dispatch) => {
return {
login: () => dispatch(login())
};
};
export default connect(mapStateToProps,mapDispatchToProps)(<ComponentName>)
Action:
export function login() {
return {
type:"TEST"
}
}
Reducer:
const initState = {
isLogged: false,
}
export default (state=initState, action) => {
switch(action.type) {
case "TEST":
return {
...state,
isLogged: true
}
break;
default:
return state
}
}
Combine Reducer:
import {combineReducers} from 'redux'
import AuthenticationReducer from './authenticationReducer'
export default combineReducers({
auth: AuthenticationReducer
})
Provider:
import React, {Component} from "react";
import <ComponentName> from './app/screens/<ComponentName>'
import store from './app/store'
import {Provider} from 'react-redux'
export default () =>
<Provider store={store}>
<<ComponentName>/>
</Provider>;
Been trying to debug this for few hours now. I am completely clueless why this is happening. Maybe I implemented it wrongly? I don't know anymore. If there are some files I forgot to include, please inform me. Thanks and have a nice day!
javascript react-native redux
1
As a side note -class <ComponentName> extends Componentlooks like incorrect syntax. Try dropping the<and>
– Dacre Denny
Nov 14 '18 at 3:13
are you usingcombineReducers()? if so, can you show that code? have you put break points throughout your code? does the switch case in your reducer hit as expected?
– Dacre Denny
Nov 14 '18 at 3:15
1
@DacreDenny Hi! Sorry for the misunderstanding, that is only a placeholder value for this question. It's not the name of the actual class :D
– seulgibear
Nov 14 '18 at 3:16
also, have you wrapped your app root component with a<Provider />?
– Dacre Denny
Nov 14 '18 at 3:16
1
@DacreDenny Oh thank you for reminding me. I will update the question right away. Thanks!
– seulgibear
Nov 14 '18 at 3:17
|
show 6 more comments
I encountered this problem when I was testing my newly created action and reducer. Prop is not being updated even though I'm setting it to a fixed value in my reducer. Here are the codes that I did in my testing.
Component:
class <ComponentName> extends Component {
componentDidMount() {
login()
}
render() {
if(this.props.isLogged)
return (
<App/>
);
else
return (
<ErrorScreen/>
);
}
}
function mapStateToProps(state) {
return {
isLogged:state.auth.isLogged
}
}
const mapDispatchToProps = (dispatch) => {
return {
login: () => dispatch(login())
};
};
export default connect(mapStateToProps,mapDispatchToProps)(<ComponentName>)
Action:
export function login() {
return {
type:"TEST"
}
}
Reducer:
const initState = {
isLogged: false,
}
export default (state=initState, action) => {
switch(action.type) {
case "TEST":
return {
...state,
isLogged: true
}
break;
default:
return state
}
}
Combine Reducer:
import {combineReducers} from 'redux'
import AuthenticationReducer from './authenticationReducer'
export default combineReducers({
auth: AuthenticationReducer
})
Provider:
import React, {Component} from "react";
import <ComponentName> from './app/screens/<ComponentName>'
import store from './app/store'
import {Provider} from 'react-redux'
export default () =>
<Provider store={store}>
<<ComponentName>/>
</Provider>;
Been trying to debug this for few hours now. I am completely clueless why this is happening. Maybe I implemented it wrongly? I don't know anymore. If there are some files I forgot to include, please inform me. Thanks and have a nice day!
javascript react-native redux
I encountered this problem when I was testing my newly created action and reducer. Prop is not being updated even though I'm setting it to a fixed value in my reducer. Here are the codes that I did in my testing.
Component:
class <ComponentName> extends Component {
componentDidMount() {
login()
}
render() {
if(this.props.isLogged)
return (
<App/>
);
else
return (
<ErrorScreen/>
);
}
}
function mapStateToProps(state) {
return {
isLogged:state.auth.isLogged
}
}
const mapDispatchToProps = (dispatch) => {
return {
login: () => dispatch(login())
};
};
export default connect(mapStateToProps,mapDispatchToProps)(<ComponentName>)
Action:
export function login() {
return {
type:"TEST"
}
}
Reducer:
const initState = {
isLogged: false,
}
export default (state=initState, action) => {
switch(action.type) {
case "TEST":
return {
...state,
isLogged: true
}
break;
default:
return state
}
}
Combine Reducer:
import {combineReducers} from 'redux'
import AuthenticationReducer from './authenticationReducer'
export default combineReducers({
auth: AuthenticationReducer
})
Provider:
import React, {Component} from "react";
import <ComponentName> from './app/screens/<ComponentName>'
import store from './app/store'
import {Provider} from 'react-redux'
export default () =>
<Provider store={store}>
<<ComponentName>/>
</Provider>;
Been trying to debug this for few hours now. I am completely clueless why this is happening. Maybe I implemented it wrongly? I don't know anymore. If there are some files I forgot to include, please inform me. Thanks and have a nice day!
javascript react-native redux
javascript react-native redux
edited Nov 14 '18 at 3:19
seulgibear
asked Nov 14 '18 at 3:08
seulgibearseulgibear
116112
116112
1
As a side note -class <ComponentName> extends Componentlooks like incorrect syntax. Try dropping the<and>
– Dacre Denny
Nov 14 '18 at 3:13
are you usingcombineReducers()? if so, can you show that code? have you put break points throughout your code? does the switch case in your reducer hit as expected?
– Dacre Denny
Nov 14 '18 at 3:15
1
@DacreDenny Hi! Sorry for the misunderstanding, that is only a placeholder value for this question. It's not the name of the actual class :D
– seulgibear
Nov 14 '18 at 3:16
also, have you wrapped your app root component with a<Provider />?
– Dacre Denny
Nov 14 '18 at 3:16
1
@DacreDenny Oh thank you for reminding me. I will update the question right away. Thanks!
– seulgibear
Nov 14 '18 at 3:17
|
show 6 more comments
1
As a side note -class <ComponentName> extends Componentlooks like incorrect syntax. Try dropping the<and>
– Dacre Denny
Nov 14 '18 at 3:13
are you usingcombineReducers()? if so, can you show that code? have you put break points throughout your code? does the switch case in your reducer hit as expected?
– Dacre Denny
Nov 14 '18 at 3:15
1
@DacreDenny Hi! Sorry for the misunderstanding, that is only a placeholder value for this question. It's not the name of the actual class :D
– seulgibear
Nov 14 '18 at 3:16
also, have you wrapped your app root component with a<Provider />?
– Dacre Denny
Nov 14 '18 at 3:16
1
@DacreDenny Oh thank you for reminding me. I will update the question right away. Thanks!
– seulgibear
Nov 14 '18 at 3:17
1
1
As a side note -
class <ComponentName> extends Component looks like incorrect syntax. Try dropping the < and >– Dacre Denny
Nov 14 '18 at 3:13
As a side note -
class <ComponentName> extends Component looks like incorrect syntax. Try dropping the < and >– Dacre Denny
Nov 14 '18 at 3:13
are you using
combineReducers() ? if so, can you show that code? have you put break points throughout your code? does the switch case in your reducer hit as expected?– Dacre Denny
Nov 14 '18 at 3:15
are you using
combineReducers() ? if so, can you show that code? have you put break points throughout your code? does the switch case in your reducer hit as expected?– Dacre Denny
Nov 14 '18 at 3:15
1
1
@DacreDenny Hi! Sorry for the misunderstanding, that is only a placeholder value for this question. It's not the name of the actual class :D
– seulgibear
Nov 14 '18 at 3:16
@DacreDenny Hi! Sorry for the misunderstanding, that is only a placeholder value for this question. It's not the name of the actual class :D
– seulgibear
Nov 14 '18 at 3:16
also, have you wrapped your app root component with a
<Provider /> ?– Dacre Denny
Nov 14 '18 at 3:16
also, have you wrapped your app root component with a
<Provider /> ?– Dacre Denny
Nov 14 '18 at 3:16
1
1
@DacreDenny Oh thank you for reminding me. I will update the question right away. Thanks!
– seulgibear
Nov 14 '18 at 3:17
@DacreDenny Oh thank you for reminding me. I will update the question right away. Thanks!
– seulgibear
Nov 14 '18 at 3:17
|
show 6 more comments
1 Answer
1
active
oldest
votes
The reason your code isn't working as expected is because you're calling the login() action creator, rather than the login() method that is returned from mapDispatchToProps() (and injected into the props of <ComponentName/>).
Try revising your code by adding this.props before your call to login() like so:
class <ComponentName> extends Component {
componentDidMount() {
// Update this line here so that the login() method
// injected by connect() is called (ie via this.props)
this.props.login()
}
render() {
if(this.props.isLogged)
return <App/>
else
return <ErrorScreen/>
}
}
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%2f53292632%2fprop-is-not-updating-as-expected-when-value-is-set-in-a-reducer%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
The reason your code isn't working as expected is because you're calling the login() action creator, rather than the login() method that is returned from mapDispatchToProps() (and injected into the props of <ComponentName/>).
Try revising your code by adding this.props before your call to login() like so:
class <ComponentName> extends Component {
componentDidMount() {
// Update this line here so that the login() method
// injected by connect() is called (ie via this.props)
this.props.login()
}
render() {
if(this.props.isLogged)
return <App/>
else
return <ErrorScreen/>
}
}
add a comment |
The reason your code isn't working as expected is because you're calling the login() action creator, rather than the login() method that is returned from mapDispatchToProps() (and injected into the props of <ComponentName/>).
Try revising your code by adding this.props before your call to login() like so:
class <ComponentName> extends Component {
componentDidMount() {
// Update this line here so that the login() method
// injected by connect() is called (ie via this.props)
this.props.login()
}
render() {
if(this.props.isLogged)
return <App/>
else
return <ErrorScreen/>
}
}
add a comment |
The reason your code isn't working as expected is because you're calling the login() action creator, rather than the login() method that is returned from mapDispatchToProps() (and injected into the props of <ComponentName/>).
Try revising your code by adding this.props before your call to login() like so:
class <ComponentName> extends Component {
componentDidMount() {
// Update this line here so that the login() method
// injected by connect() is called (ie via this.props)
this.props.login()
}
render() {
if(this.props.isLogged)
return <App/>
else
return <ErrorScreen/>
}
}
The reason your code isn't working as expected is because you're calling the login() action creator, rather than the login() method that is returned from mapDispatchToProps() (and injected into the props of <ComponentName/>).
Try revising your code by adding this.props before your call to login() like so:
class <ComponentName> extends Component {
componentDidMount() {
// Update this line here so that the login() method
// injected by connect() is called (ie via this.props)
this.props.login()
}
render() {
if(this.props.isLogged)
return <App/>
else
return <ErrorScreen/>
}
}
answered Nov 14 '18 at 3:28
Dacre DennyDacre Denny
11.4k41030
11.4k41030
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%2f53292632%2fprop-is-not-updating-as-expected-when-value-is-set-in-a-reducer%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
1
As a side note -
class <ComponentName> extends Componentlooks like incorrect syntax. Try dropping the<and>– Dacre Denny
Nov 14 '18 at 3:13
are you using
combineReducers()? if so, can you show that code? have you put break points throughout your code? does the switch case in your reducer hit as expected?– Dacre Denny
Nov 14 '18 at 3:15
1
@DacreDenny Hi! Sorry for the misunderstanding, that is only a placeholder value for this question. It's not the name of the actual class :D
– seulgibear
Nov 14 '18 at 3:16
also, have you wrapped your app root component with a
<Provider />?– Dacre Denny
Nov 14 '18 at 3:16
1
@DacreDenny Oh thank you for reminding me. I will update the question right away. Thanks!
– seulgibear
Nov 14 '18 at 3:17