Replace Legacy Root API's Callback Argument

Michael Chan
InstructorMichael Chan
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

React's legacy root API (render) took a callback function. React called this function once the post-render. With new streaming capabilities scheduled for React 18, this feature becomes unpredictable. Replace it with a ref callback — or one of these other strategies.

Instructor: [0:01] ReactDOM's legacy root API allowed us to pass in a callback function that would get called as soon as React rendered. It would get passed in something like console.log("React rendered")

[0:12] If we try that with the new createRoot API, we get an error, "Warning -- Render does not support the second callback argument. To execute a site effect after rendering, declare it in a component body with useEffect()."

[0:26] There's several recommendations for how to solve this, but let's follow this warning first. Let's take out the second argument and move it into our apps component body with a useEffect(). Paste it in, and our error goes away. We also see this rendered out to the console.

[0:47] With a legacy root API, this only rendered the first time. To get parity with that, we would need to add an empty array to this useEffect(). Doesn't change the result except for the fact that it will only render on that first render.

[1:01] Of course, there's another option if you don't want to modify this component. Let's copy this, comment that out. We can use setTimeout() or requestIdleCallback() right after calling ReactDOM.createRoot(). window.setTimeout() -- handy little autocomplete there -- you change this to zero and paste in our function. Clear this that we don't need.

[1:27] What this does is a little bit different. Get the same result in this simplified app, but this version isn't precisely coordinated with React rendering. It will simply render as soon as React yields two low-priority updates.

[1:42] There's a third option that might come in handy to you if you need a ref to the DOM node that React rendered to. Here, we can provide a callback as a prop, take the ref as an argument, and log it out, ref.tagName.

[2:00] On the component side, we take that callback as a prop and use ref on this React element. Save, refresh, and we have access to that DOM element that React rendered.

egghead
egghead
~ 37 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today