score:1

your ternary is checking the state for showfirstcomponent and rendering only one.

{this.state.showfirstcomponent ? c1 : c2}

if you remove that all together, and just do

<simplecomponent number="1" />
<simplecomponent number="2" />

you'll get two logs. to further answer the question, constructor is called when the component is mounted, so usually once, unless you're dynamically mounting stuff. to get back at your ternary:

{this.state.showfirstcomponent ? c1 : c2}

if you were to toggle this.state.showfirstcomponent with a button lets say:

<button onclick={() => this.setstate({ 
    showfirstcomponent: !this.state.showfirstcomponent
})> 
  toggle c1/c2 
</button>

every time you hit this button, component will be unmounted, and another one mounted, so you'll get a console log. here's the order of react lifecycle methods between mount and unmount. however, if you do anything else, eg change the props passed, change the internal component state of <simplecomponent />, it will not unmount, but rather, update, which calls the render method, and not the constructor. i suggest adding a console.log for some lifecycle methods in your <simplecomponent /> and trying to trigger them, to better understand the component lifecycle. or read an article about it.

score:2

if someone has landed here for react-native. assigning different keys for different instances will create new instances instead of reusing the same instance.

   <mycomponent
       key={this.state.uniqueinstancekey}
       mydata={this.state.mydata}/>

score:4

according to the doc, constructor is only called before a component is mounted. if you add two more react lifecycle functions to your code example: componentdidmount and componentwillunmount, you'll see that the component only mounted once in the entire process, regardless of the times the state changes. if you then add shouldcomponentupdate to the component, you'll see that there is only one instance of simplecomponent and the upper-level state change is shown as props change on simplecomponent.

the reason why you don't see two instances mounting and unmounting here is that variables c1 and c2 are not instances of simplecomponent as commonly and conveniently assumed. instead, they are reactelement, which is a description of the component instance.

the primary type in react is the reactelement. it has four properties: type, props, key and ref. it has no methods and nothing on the prototype.

react (virtual) dom terminology

only when the element gets rendered, an instance of the component gets created and constructor and the lifecycle functions get called at their respective time points.

so then why, as in your example, the element c2 never gets to be used to create a component? this goes back to how react decides when and what to update at a certain time (you can read more about it here:

when a component updates, the instance stays the same, so that state is maintained across renders.

in your example, when the app component renders c2 instead of c1, react sees that the new component element is the same as the old one so it reuses the component(i.e. only a single instance exists) and updates the props/states accordingly.

here are some more readings that i find useful to understand this problem: https://developmentarc.gitbooks.io/react-indepth/content/life_cycle/birth_mounting_indepth.html

mark amery's answer to this question is also very helpful.


Related Query

More Query from same tag