T. Friedrichsen
T. Friedrichsen

Reputation: 149

componentDidMount only fires once when rendering multiple Component via map()

I am facing a weird problem. I've written a React component which renders multiple custom child components via the array.map()-method like this:

public render(): React.ReactElement<IParentProps> {
   return (
       this.children.map((item, idx) => {
           return (<Child customProperty={item}>);
       }
   );
}

All of my children get rendered, but componentDidMount gets called just once and not, like I would like it to be, every time a Child components gets created. It's crazy. I am using React 15.6.2 (can't upgrade, it's a, SharePoint 2019 solution).

Upvotes: 0

Views: 160

Answers (2)

lewisnewson
lewisnewson

Reputation: 460

To further Murats answer, what you're likely looking for is componentDidUpdate()

Upvotes: 1

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37604

You are misunderstanding the lifecycle hook. componentDidMount means as the name suggests that the component is mounted (does not mean the render() function is done). It also does not get called for rendering elements at all.

Upvotes: 1

Related Questions