Skip to content

React Trigger vs Render vs Commit vs Paint

Triggering, rendering, committing, and painting are the steps involved in displaying React UIs on screen. Here are the differences between the four steps:

React Component's
Lifecyle

The four phases in the lifecycle of a component’s UI

  • Trigger: Specifies the component whose UI you want to display on the screen.
  • Render: Calls the component you’ve triggered.
  • Commit: Updates the DOM with the UI of the rendered component.
  • Paint: Converts the DOM code you’ve committed to user-friendly elements that users can interact with in their browser.

Let’s discuss these differences in detail.

The trigger event is the first step in displaying a React user interface (UI) on screen. It specifies the component whose UI you want to display on the screen. This event happens on two occasions:

  1. When the app starts running. The React application uses the createRoot method to specify the component whose UI you want to render to a DOM node.
import ReactDOM from "react-dom/client";
import DonationUI from "./components/DonationUI.js";
// When the app starts running, you need to use createRoot and its render method to trigger an initial rendering of the app's root component.
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<DonationUI />);
  1. The second reason a trigger event can happen is when a component’s (or its ancestors’) state gets updated with a set function.
import React from "react";
function DonationUI() {
const [donate, setDonate] = React.useState(false);
function createUserInterface() {
if (donate) {
return (
<p>
<a href="https://www.buymeacoffee.com/codesweetly">Support page</a>.
Thank you so much! 🎉
</p>
);
}
// The setDonate function will trigger a re-rendering of the DonationUI component.
return <button onClick={() => setDonate(true)}>☕ Buy me a coffee</button>;
}
return createUserInterface();
}
export default DonationUI;

The rendering step is when React invokes (calls) the component you’ve triggered with either the createRoot method or a set function. Rendering causes React to invoke a component to produce the UI you want to display on the screen. The component React will render depending on the moment the trigger event occurred:

  • For an initial trigger (at the start of the app), React will invoke the app’s root component.
  • For update triggers (whenever a component’s state gets updated), React will call the function component whose state update initiated the trigger event.

Committing React UI to the Browser’s DOM

Section titled “Committing React UI to the Browser’s DOM”

The committing stage is when React updates the DOM with the UI of the rendered component.

Note the following:

  • For an initial render (at the start of the app), React will initialize the root DOM with the root component’s UI. It will use the appendChild() JavaScript API to append the DOM nodes (UI) retrieved from the component into the app’s root HTML element.
  • For subsequent re-rendering (after the initial commit), React will only update the DOM nodes if there’s a difference between the previous rendering output and the latest one. No changes will occur if the component’s latest output is the same as the previously committed one.

The painting (browser rendering) stage is when the browser repaints the screen to convert the DOM code to user-friendly elements. This is a browser-level process that begins once React has finished updating (committing) the DOM nodes.