1👍
The render()
method should be a pure function, which means that it should not directly change the component state. You should either change your state in a lifecycle method (for example, componentDidMount()
) or a custom method (event handler) that you invoke yourself at some point in the render.
Here is a CodeSandbox demonstrating this idea.
I recommend going through the lifecycle methods and figuring out which one fits your purpose best.
Source:stackexchange.com