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









share|improve this question


















  • 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















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









share|improve this question


















  • 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













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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












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.






share|improve this answer





















  • this doesn't work either
    – hitchhiker
    Nov 12 at 21:18











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',
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%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

























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.






share|improve this answer





















  • this doesn't work either
    – hitchhiker
    Nov 12 at 21:18















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.






share|improve this answer





















  • this doesn't work either
    – hitchhiker
    Nov 12 at 21:18













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 21:16









Juust

407




407












  • 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




this doesn't work either
– hitchhiker
Nov 12 at 21:18


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?