How it works...

With componentDidMount, we avoided the infinite loop. The reason why this is a better approach is that componentDidMount is being executed just once when the component is already mounted, and in that method, we are executing our setTimeout and updating the name state only once. In the following recipes, we are going to learn more about React lifecycle methods.

Get React Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.