How to get the props to update in redux using a variable outside `return`?
up vote
0
down vote
favorite
I'm using React and Redux to build a website and I have the code below for AppInfo
component. I have result
variable in the render
method which is initialized before return statement. At first the result
is an empty string but once the state changes it is set to an object which contains various app attributes. I want to display the result
variable inside a div
. The result
always stays an empty string although I do see that the redux state is updated. In addition if call directly this.props.appId
inside the div
the updated state is indeed displayed. Why the result variable doesn't work?
This is my code:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { connect } from 'react-redux';
class AppInfo extends Component {
render() {
const { appInfo } = this.props;
const { appId } = appInfo;
let result = '';
if (appId) {
result = JSON.stringify(appInfo);
}
return (
<div>
{result}
{`JSON.stringify(appInfo) = ${JSON.stringify(appInfo)}`}
{`this.props = ${JSON.stringify(this.props.appInfo)}`}
</div>
);
}
}
AppInfo.propTypes = {
appInfo: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
appInfo: state.app
});
export default connect(mapStateToProps, null)(AppInfo);
javascript reactjs redux
add a comment |
up vote
0
down vote
favorite
I'm using React and Redux to build a website and I have the code below for AppInfo
component. I have result
variable in the render
method which is initialized before return statement. At first the result
is an empty string but once the state changes it is set to an object which contains various app attributes. I want to display the result
variable inside a div
. The result
always stays an empty string although I do see that the redux state is updated. In addition if call directly this.props.appId
inside the div
the updated state is indeed displayed. Why the result variable doesn't work?
This is my code:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { connect } from 'react-redux';
class AppInfo extends Component {
render() {
const { appInfo } = this.props;
const { appId } = appInfo;
let result = '';
if (appId) {
result = JSON.stringify(appInfo);
}
return (
<div>
{result}
{`JSON.stringify(appInfo) = ${JSON.stringify(appInfo)}`}
{`this.props = ${JSON.stringify(this.props.appInfo)}`}
</div>
);
}
}
AppInfo.propTypes = {
appInfo: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
appInfo: state.app
});
export default connect(mapStateToProps, null)(AppInfo);
javascript reactjs redux
1
What does the appInfo object look like? Do you have a property called appId inside that object? You say that if you call this.props.appId you get the desired result but I don't see that is possible as you don't have a prop that is named appId. Or do you have that also? It is probably something wrong with the appId destructuring and the if statement that checks for appId to be true.
– weibenfalk
Nov 12 at 21:15
The problem was indeed with incorrect destructuring
– hitchhiker
Nov 12 at 21:25
What does your reducer look like?
– markerikson
Nov 12 at 21:27
Glad to hear that it worked out!
– weibenfalk
Nov 12 at 23:56
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using React and Redux to build a website and I have the code below for AppInfo
component. I have result
variable in the render
method which is initialized before return statement. At first the result
is an empty string but once the state changes it is set to an object which contains various app attributes. I want to display the result
variable inside a div
. The result
always stays an empty string although I do see that the redux state is updated. In addition if call directly this.props.appId
inside the div
the updated state is indeed displayed. Why the result variable doesn't work?
This is my code:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { connect } from 'react-redux';
class AppInfo extends Component {
render() {
const { appInfo } = this.props;
const { appId } = appInfo;
let result = '';
if (appId) {
result = JSON.stringify(appInfo);
}
return (
<div>
{result}
{`JSON.stringify(appInfo) = ${JSON.stringify(appInfo)}`}
{`this.props = ${JSON.stringify(this.props.appInfo)}`}
</div>
);
}
}
AppInfo.propTypes = {
appInfo: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
appInfo: state.app
});
export default connect(mapStateToProps, null)(AppInfo);
javascript reactjs redux
I'm using React and Redux to build a website and I have the code below for AppInfo
component. I have result
variable in the render
method which is initialized before return statement. At first the result
is an empty string but once the state changes it is set to an object which contains various app attributes. I want to display the result
variable inside a div
. The result
always stays an empty string although I do see that the redux state is updated. In addition if call directly this.props.appId
inside the div
the updated state is indeed displayed. Why the result variable doesn't work?
This is my code:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { connect } from 'react-redux';
class AppInfo extends Component {
render() {
const { appInfo } = this.props;
const { appId } = appInfo;
let result = '';
if (appId) {
result = JSON.stringify(appInfo);
}
return (
<div>
{result}
{`JSON.stringify(appInfo) = ${JSON.stringify(appInfo)}`}
{`this.props = ${JSON.stringify(this.props.appInfo)}`}
</div>
);
}
}
AppInfo.propTypes = {
appInfo: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
appInfo: state.app
});
export default connect(mapStateToProps, null)(AppInfo);
javascript reactjs redux
javascript reactjs redux
asked Nov 12 at 20:58
hitchhiker
656
656
1
What does the appInfo object look like? Do you have a property called appId inside that object? You say that if you call this.props.appId you get the desired result but I don't see that is possible as you don't have a prop that is named appId. Or do you have that also? It is probably something wrong with the appId destructuring and the if statement that checks for appId to be true.
– weibenfalk
Nov 12 at 21:15
The problem was indeed with incorrect destructuring
– hitchhiker
Nov 12 at 21:25
What does your reducer look like?
– markerikson
Nov 12 at 21:27
Glad to hear that it worked out!
– weibenfalk
Nov 12 at 23:56
add a comment |
1
What does the appInfo object look like? Do you have a property called appId inside that object? You say that if you call this.props.appId you get the desired result but I don't see that is possible as you don't have a prop that is named appId. Or do you have that also? It is probably something wrong with the appId destructuring and the if statement that checks for appId to be true.
– weibenfalk
Nov 12 at 21:15
The problem was indeed with incorrect destructuring
– hitchhiker
Nov 12 at 21:25
What does your reducer look like?
– markerikson
Nov 12 at 21:27
Glad to hear that it worked out!
– weibenfalk
Nov 12 at 23:56
1
1
What does the appInfo object look like? Do you have a property called appId inside that object? You say that if you call this.props.appId you get the desired result but I don't see that is possible as you don't have a prop that is named appId. Or do you have that also? It is probably something wrong with the appId destructuring and the if statement that checks for appId to be true.
– weibenfalk
Nov 12 at 21:15
What does the appInfo object look like? Do you have a property called appId inside that object? You say that if you call this.props.appId you get the desired result but I don't see that is possible as you don't have a prop that is named appId. Or do you have that also? It is probably something wrong with the appId destructuring and the if statement that checks for appId to be true.
– weibenfalk
Nov 12 at 21:15
The problem was indeed with incorrect destructuring
– hitchhiker
Nov 12 at 21:25
The problem was indeed with incorrect destructuring
– hitchhiker
Nov 12 at 21:25
What does your reducer look like?
– markerikson
Nov 12 at 21:27
What does your reducer look like?
– markerikson
Nov 12 at 21:27
Glad to hear that it worked out!
– weibenfalk
Nov 12 at 23:56
Glad to hear that it worked out!
– weibenfalk
Nov 12 at 23:56
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Could you try to use result = JSON.stringify(this.props.appInfo);
?
I think the issue might be related to the first 2 const’s you create.
this doesn't work either
– hitchhiker
Nov 12 at 21:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Could you try to use result = JSON.stringify(this.props.appInfo);
?
I think the issue might be related to the first 2 const’s you create.
this doesn't work either
– hitchhiker
Nov 12 at 21:18
add a comment |
up vote
0
down vote
Could you try to use result = JSON.stringify(this.props.appInfo);
?
I think the issue might be related to the first 2 const’s you create.
this doesn't work either
– hitchhiker
Nov 12 at 21:18
add a comment |
up vote
0
down vote
up vote
0
down vote
Could you try to use result = JSON.stringify(this.props.appInfo);
?
I think the issue might be related to the first 2 const’s you create.
Could you try to use result = JSON.stringify(this.props.appInfo);
?
I think the issue might be related to the first 2 const’s you create.
answered Nov 12 at 21:16
Juust
407
407
this doesn't work either
– hitchhiker
Nov 12 at 21:18
add a comment |
this doesn't work either
– hitchhiker
Nov 12 at 21:18
this doesn't work either
– hitchhiker
Nov 12 at 21:18
this doesn't work either
– hitchhiker
Nov 12 at 21:18
add a comment |
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%2f53269996%2fhow-to-get-the-props-to-update-in-redux-using-a-variable-outside-return%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
What does the appInfo object look like? Do you have a property called appId inside that object? You say that if you call this.props.appId you get the desired result but I don't see that is possible as you don't have a prop that is named appId. Or do you have that also? It is probably something wrong with the appId destructuring and the if statement that checks for appId to be true.
– weibenfalk
Nov 12 at 21:15
The problem was indeed with incorrect destructuring
– hitchhiker
Nov 12 at 21:25
What does your reducer look like?
– markerikson
Nov 12 at 21:27
Glad to hear that it worked out!
– weibenfalk
Nov 12 at 23:56