Mounting is the process to render the component for the first time. This process includes everything necessary for the initial load — even if data needs to be loaded asynchronously.
To make mounting work, we need to give users a callback to run after the initial mount. They can then use setState inside afterMount to trigger a re-render.
Here’s the code we can use in Child to set the parent’s childCount value.
Since mounting only happens once — during the initial load — we want to make sure we don’t run afterMount again. We can do this by checking with a Boolean called mounted. We’ll set mounted to false initially.