React: What is a React element?
23 October 2022 (Updated 23 October 2022)
On this page
In a nutshell
In the host environment, a host instance (e.g., HTMLDivElement) is the smallest building block. In React, the smallest building block is a React element β a plain JavaScript object that describes a host instance.
// JSX is a syntax sugar for these objects.
// <button className="blue" />
{
type: 'button',
props: { className: 'blue' }
}
Like host instances, React elements can form a tree:
// JSX is a syntax sugar for these objects.
// <dialog>
// <button className="blue" />
// <button className="red" />
// </dialog>
{
type: 'dialog',
props: {
children: [{
type: 'button',
props: { className: 'blue' }
}, {
type: 'button',
props: { className: 'red' }
}]
}
}
React elements are immutable. You donβt change its properties or children. If you want to render something different, you replace it with a new React element.
React elements can be thought of like frames in a movie. They capture what the UI should look like at a specific point in time.
Sources
Tagged:
React
Thanks for your comment π. Once it's approved, it will appear here.
Leave a comment