You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add note about useEffect to setVariable and setTheme
The reason this code isn’t correct is that it tries to do something with the DOM node during rendering. In React, rendering should be a pure calculation of JSX and should not contain side effects like modifying the DOM.
Moreover, when VideoPlayer is called for the first time, its DOM does not exist yet! There isn’t a DOM node yet to call play() or pause() on, because React doesn’t know what DOM to create until you return the JSX.
The solution here is to wrap the side effect with useEffect to move it out of the rendering calculation:
By wrapping the DOM update in an Effect, you let React update the screen first. Then your Effect runs.
When your VideoPlayer component renders (either the first time or if it re-renders), a few things will happen. First, React will update the screen, ensuring the
The text was updated successfully, but these errors were encountered:
useEffect
tosetVariable
andsetTheme
The reason this code isn’t correct is that it tries to do something with the DOM node during rendering. In React, rendering should be a pure calculation of JSX and should not contain side effects like modifying the DOM.
Moreover, when VideoPlayer is called for the first time, its DOM does not exist yet! There isn’t a DOM node yet to call play() or pause() on, because React doesn’t know what DOM to create until you return the JSX.
The solution here is to wrap the side effect with useEffect to move it out of the rendering calculation:
By wrapping the DOM update in an Effect, you let React update the screen first. Then your Effect runs.
When your VideoPlayer component renders (either the first time or if it re-renders), a few things will happen. First, React will update the screen, ensuring the
The text was updated successfully, but these errors were encountered: