React will not create a new nested component as expected












-4















In my application I have a page change mechanism that uses one of the properties of parent component state.



So I do something like:



class mainComponent{
state={
mypage:null
}

onclickhandler(page){
this.setState({mypage:page});
}

render(){
return <div>{page}</div>
};
}


In my "page" property I have various input fields and other components.



When I change the page, all inputs get updated with new values. However, my custom nested components are never re-created.



Instead, reach just calls componentDidUpdate on them.



Is there a way to force it to construct a new nested component somehow?










share|improve this question

























  • Shouldn't onclickhandler(page){ be like onclickhandler = (page) => {?

    – NoobieSatan
    Nov 21 '18 at 19:57











  • Plus I guess state can't have ; , it should probably be like state={ mypage:null }( without ;)

    – NoobieSatan
    Nov 21 '18 at 19:59











  • There is just so much wrong with your code. You are having mypage in state and then you are doing this.setState(page) in your handler, You should probably do something like this this.setState({mypage: page}) Also What is page when you did something like this <div>{page}</div>?

    – NoobieSatan
    Nov 21 '18 at 20:01













  • i corrected those typos... question is still relevant

    – AnKing
    Nov 21 '18 at 20:28






  • 1





    there is too many missing details, create Minimal, Complete, and Verifiable example

    – xadm
    Nov 21 '18 at 20:53
















-4















In my application I have a page change mechanism that uses one of the properties of parent component state.



So I do something like:



class mainComponent{
state={
mypage:null
}

onclickhandler(page){
this.setState({mypage:page});
}

render(){
return <div>{page}</div>
};
}


In my "page" property I have various input fields and other components.



When I change the page, all inputs get updated with new values. However, my custom nested components are never re-created.



Instead, reach just calls componentDidUpdate on them.



Is there a way to force it to construct a new nested component somehow?










share|improve this question

























  • Shouldn't onclickhandler(page){ be like onclickhandler = (page) => {?

    – NoobieSatan
    Nov 21 '18 at 19:57











  • Plus I guess state can't have ; , it should probably be like state={ mypage:null }( without ;)

    – NoobieSatan
    Nov 21 '18 at 19:59











  • There is just so much wrong with your code. You are having mypage in state and then you are doing this.setState(page) in your handler, You should probably do something like this this.setState({mypage: page}) Also What is page when you did something like this <div>{page}</div>?

    – NoobieSatan
    Nov 21 '18 at 20:01













  • i corrected those typos... question is still relevant

    – AnKing
    Nov 21 '18 at 20:28






  • 1





    there is too many missing details, create Minimal, Complete, and Verifiable example

    – xadm
    Nov 21 '18 at 20:53














-4












-4








-4








In my application I have a page change mechanism that uses one of the properties of parent component state.



So I do something like:



class mainComponent{
state={
mypage:null
}

onclickhandler(page){
this.setState({mypage:page});
}

render(){
return <div>{page}</div>
};
}


In my "page" property I have various input fields and other components.



When I change the page, all inputs get updated with new values. However, my custom nested components are never re-created.



Instead, reach just calls componentDidUpdate on them.



Is there a way to force it to construct a new nested component somehow?










share|improve this question
















In my application I have a page change mechanism that uses one of the properties of parent component state.



So I do something like:



class mainComponent{
state={
mypage:null
}

onclickhandler(page){
this.setState({mypage:page});
}

render(){
return <div>{page}</div>
};
}


In my "page" property I have various input fields and other components.



When I change the page, all inputs get updated with new values. However, my custom nested components are never re-created.



Instead, reach just calls componentDidUpdate on them.



Is there a way to force it to construct a new nested component somehow?







reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:36









c-chavez

2,28221733




2,28221733










asked Nov 21 '18 at 19:55









AnKingAnKing

373513




373513













  • Shouldn't onclickhandler(page){ be like onclickhandler = (page) => {?

    – NoobieSatan
    Nov 21 '18 at 19:57











  • Plus I guess state can't have ; , it should probably be like state={ mypage:null }( without ;)

    – NoobieSatan
    Nov 21 '18 at 19:59











  • There is just so much wrong with your code. You are having mypage in state and then you are doing this.setState(page) in your handler, You should probably do something like this this.setState({mypage: page}) Also What is page when you did something like this <div>{page}</div>?

    – NoobieSatan
    Nov 21 '18 at 20:01













  • i corrected those typos... question is still relevant

    – AnKing
    Nov 21 '18 at 20:28






  • 1





    there is too many missing details, create Minimal, Complete, and Verifiable example

    – xadm
    Nov 21 '18 at 20:53



















  • Shouldn't onclickhandler(page){ be like onclickhandler = (page) => {?

    – NoobieSatan
    Nov 21 '18 at 19:57











  • Plus I guess state can't have ; , it should probably be like state={ mypage:null }( without ;)

    – NoobieSatan
    Nov 21 '18 at 19:59











  • There is just so much wrong with your code. You are having mypage in state and then you are doing this.setState(page) in your handler, You should probably do something like this this.setState({mypage: page}) Also What is page when you did something like this <div>{page}</div>?

    – NoobieSatan
    Nov 21 '18 at 20:01













  • i corrected those typos... question is still relevant

    – AnKing
    Nov 21 '18 at 20:28






  • 1





    there is too many missing details, create Minimal, Complete, and Verifiable example

    – xadm
    Nov 21 '18 at 20:53

















Shouldn't onclickhandler(page){ be like onclickhandler = (page) => {?

– NoobieSatan
Nov 21 '18 at 19:57





Shouldn't onclickhandler(page){ be like onclickhandler = (page) => {?

– NoobieSatan
Nov 21 '18 at 19:57













Plus I guess state can't have ; , it should probably be like state={ mypage:null }( without ;)

– NoobieSatan
Nov 21 '18 at 19:59





Plus I guess state can't have ; , it should probably be like state={ mypage:null }( without ;)

– NoobieSatan
Nov 21 '18 at 19:59













There is just so much wrong with your code. You are having mypage in state and then you are doing this.setState(page) in your handler, You should probably do something like this this.setState({mypage: page}) Also What is page when you did something like this <div>{page}</div>?

– NoobieSatan
Nov 21 '18 at 20:01







There is just so much wrong with your code. You are having mypage in state and then you are doing this.setState(page) in your handler, You should probably do something like this this.setState({mypage: page}) Also What is page when you did something like this <div>{page}</div>?

– NoobieSatan
Nov 21 '18 at 20:01















i corrected those typos... question is still relevant

– AnKing
Nov 21 '18 at 20:28





i corrected those typos... question is still relevant

– AnKing
Nov 21 '18 at 20:28




1




1





there is too many missing details, create Minimal, Complete, and Verifiable example

– xadm
Nov 21 '18 at 20:53





there is too many missing details, create Minimal, Complete, and Verifiable example

– xadm
Nov 21 '18 at 20:53












1 Answer
1






active

oldest

votes


















1














React won't recreate component when it's not needed - it's a part of optimizations and general way react works.



When 'new', 'expected' component exists (the same type, the same or no key id) it only receives updated props.



Updating props sometimes doesn't force rerendering (it's guaranteed for state updates). PureComponent compares props shallowly, you can check shouldComponentUpdate (should return true to force render).






share|improve this answer


























  • How can you use componentShouldUpdate to force re-render?

    – NoobieSatan
    Nov 21 '18 at 20:56











  • I ended up using key to re-render the internal components. Thanks for the solution!

    – AnKing
    Nov 21 '18 at 21:22











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419615%2freact-will-not-create-a-new-nested-component-as-expected%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









1














React won't recreate component when it's not needed - it's a part of optimizations and general way react works.



When 'new', 'expected' component exists (the same type, the same or no key id) it only receives updated props.



Updating props sometimes doesn't force rerendering (it's guaranteed for state updates). PureComponent compares props shallowly, you can check shouldComponentUpdate (should return true to force render).






share|improve this answer


























  • How can you use componentShouldUpdate to force re-render?

    – NoobieSatan
    Nov 21 '18 at 20:56











  • I ended up using key to re-render the internal components. Thanks for the solution!

    – AnKing
    Nov 21 '18 at 21:22
















1














React won't recreate component when it's not needed - it's a part of optimizations and general way react works.



When 'new', 'expected' component exists (the same type, the same or no key id) it only receives updated props.



Updating props sometimes doesn't force rerendering (it's guaranteed for state updates). PureComponent compares props shallowly, you can check shouldComponentUpdate (should return true to force render).






share|improve this answer


























  • How can you use componentShouldUpdate to force re-render?

    – NoobieSatan
    Nov 21 '18 at 20:56











  • I ended up using key to re-render the internal components. Thanks for the solution!

    – AnKing
    Nov 21 '18 at 21:22














1












1








1







React won't recreate component when it's not needed - it's a part of optimizations and general way react works.



When 'new', 'expected' component exists (the same type, the same or no key id) it only receives updated props.



Updating props sometimes doesn't force rerendering (it's guaranteed for state updates). PureComponent compares props shallowly, you can check shouldComponentUpdate (should return true to force render).






share|improve this answer















React won't recreate component when it's not needed - it's a part of optimizations and general way react works.



When 'new', 'expected' component exists (the same type, the same or no key id) it only receives updated props.



Updating props sometimes doesn't force rerendering (it's guaranteed for state updates). PureComponent compares props shallowly, you can check shouldComponentUpdate (should return true to force render).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 21:13

























answered Nov 21 '18 at 20:48









xadmxadm

1,615248




1,615248













  • How can you use componentShouldUpdate to force re-render?

    – NoobieSatan
    Nov 21 '18 at 20:56











  • I ended up using key to re-render the internal components. Thanks for the solution!

    – AnKing
    Nov 21 '18 at 21:22



















  • How can you use componentShouldUpdate to force re-render?

    – NoobieSatan
    Nov 21 '18 at 20:56











  • I ended up using key to re-render the internal components. Thanks for the solution!

    – AnKing
    Nov 21 '18 at 21:22

















How can you use componentShouldUpdate to force re-render?

– NoobieSatan
Nov 21 '18 at 20:56





How can you use componentShouldUpdate to force re-render?

– NoobieSatan
Nov 21 '18 at 20:56













I ended up using key to re-render the internal components. Thanks for the solution!

– AnKing
Nov 21 '18 at 21:22





I ended up using key to re-render the internal components. Thanks for the solution!

– AnKing
Nov 21 '18 at 21:22




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419615%2freact-will-not-create-a-new-nested-component-as-expected%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory