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.