admin管理员组文章数量:1392003
Im new in ReactJS and I'm having problem with the concept of state and re-render of ponent.
In my parent ponent, I have this state in constructor:
this.state = {
locale : 'en',
messages : ENGLISH.messages,
softwareInfo : {
software_name : 'Test Default 1.0',
default_dash : 1,
color_theme: '#009900'
}
}
In the Render, I load a ponent called DashAppBar passing the softwareInfo:
<DashAppBar softwareInfo = { this.state.softwareInfo} />
In DashAppBar construct, I read that state as property:
this.state = {
software_settings: props.softwareInfo,
[...]
}
And then I print the software name in the bar with this:
const software_name = (
<FormattedMessage
id="software_name"
defaultMessage="{software_name}"
values={{software_name:this.state.software_settings.software_name}}
/>
);
This work fine.
Unfortunally, parent ponent make an AJAX call that change softwareInfo:
success: function(result){
//window.software_settings = $.parseJSON(result);
//window.software_settings = result;
var settings = {
software_name:result.software_name,
default_dash:result.default_dash,
color_theme:result.color_theme
};
this.setState({softwareInfo : settings});
}
Calling setState should re-render the ponent but the child value remain the same as original and the new value got from AjaxCall are not displayed.
Where is the error? Change the parent state should update the child.
Thanks!
Im new in ReactJS and I'm having problem with the concept of state and re-render of ponent.
In my parent ponent, I have this state in constructor:
this.state = {
locale : 'en',
messages : ENGLISH.messages,
softwareInfo : {
software_name : 'Test Default 1.0',
default_dash : 1,
color_theme: '#009900'
}
}
In the Render, I load a ponent called DashAppBar passing the softwareInfo:
<DashAppBar softwareInfo = { this.state.softwareInfo} />
In DashAppBar construct, I read that state as property:
this.state = {
software_settings: props.softwareInfo,
[...]
}
And then I print the software name in the bar with this:
const software_name = (
<FormattedMessage
id="software_name"
defaultMessage="{software_name}"
values={{software_name:this.state.software_settings.software_name}}
/>
);
This work fine.
Unfortunally, parent ponent make an AJAX call that change softwareInfo:
success: function(result){
//window.software_settings = $.parseJSON(result);
//window.software_settings = result;
var settings = {
software_name:result.software_name,
default_dash:result.default_dash,
color_theme:result.color_theme
};
this.setState({softwareInfo : settings});
}
Calling setState should re-render the ponent but the child value remain the same as original and the new value got from AjaxCall are not displayed.
Where is the error? Change the parent state should update the child.
Thanks!
Share Improve this question asked Jan 29, 2018 at 13:35 RetroMimeRetroMime 3872 gold badges9 silver badges24 bronze badges 2- Ajax call gets triggered correctly ? – Mosè Raguzzini Commented Jan 29, 2018 at 13:39
-
Unless you are going to mutate that property in the child ponent, don't put it in
State
. Instead just read it straight fromprops
. – tgallacher Commented Jan 29, 2018 at 13:44
2 Answers
Reset to default 4The issue is that you're passing the parent's state only in the child's constructor. If the parent updates props you either need to do ponentWillReceiveProps and update the child state or just use the software_settings
as prop directly.
For example, in the child ponent:
const software_name = (
<FormattedMessage
id="software_name"
defaultMessage="{software_name}"
values={{software_name:this.props.software_settings.software_name}}
/>
);
or with ponentWillReceiveProps:
ponentWillReceiveProps(nextProps) {
this.setState({ software_settings: nextProps.software_settings});
}
you need to set a ponentWillReceiveProps
hook on your child ponent to updated it. https://reactjs/docs/react-ponent.html#ponentwillreceiveprops
本文标签: javascriptReactJsChild not updating after Parent State changedStack Overflow
版权声明:本文标题:javascript - ReactJs - Child not updating after Parent State changed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744684490a2619615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论