React – Warning: Cannot update a component while rendering a different component

React

React lets you build user interfaces out of individual pieces called components. Create your own React components like Thumbnail, LikeButton, and Video. Then combine them into entire screens, pages, and apps.

How to reproduce it


<Element onClick={ setOutput('') } ></Element>

Error

Warning: Cannot update a component (`App`) while rendering a different component (`ElementComponent`). To locate the bad setState() call inside `ElementComponent`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
    at ElementComponent (http://localhost:3000/static/js/bundle.js:175:5)
    at div
    at App (http://localhost:3000/static/js/bundle.js:33:88)

Solution

Use the arrow function for the function call. You cannot call the function directly from the event.

<Element onClick={ () => { setOutput('') } } ></Element>
https://www.mldgroup.com

Vyštudovaný top manažér pôsobiaci najmä ako manažér, marketér, softvérový inžinier, konzultant, bloger, YouTuber a zatiaľ neúspešný hudobník a producent. V rámci praxe pôsobil v rôznych odvetviach na rôznych pozíciách v malých aj veľkých firmách, vrátane spoluprác a partnerstiev s významnými firmami či poradenskými spoločnosťami.

Pridaj komentár

Vaša e-mailová adresa nebude zverejnená. Vyžadované polia sú označené *