Prelude
We are all familiar with the default renderers that comes with React - ReactDOM.
We also have custom React renderers that can be used to interface with "hosts" that aren't the DOM environment, such as mobile devices (famously React Native), VR devices, the terminal (like ink), and so on.
However, there are some custom renderers that operate within (or instead of) ReactDOM while in a browser host.
Core question
What is the purpose of custom renderers that operate within the browser host?
Examples
Some notable examples of custom renderers that operate within the browser host are:
The following snippet from react-three-fiber
threejs elements are declared in React and under the hood it will "map" to particular canvas operations:
<Canvas>
<ambientLight />
<pointLight position={[10, 10, 10]} />
<Box position={[-1.2, 0, 0]} />
<Box position={[1.2, 0, 0]} />
</Canvas>
react-three-fiber
is using a custom renderer to achieve the above, but I think it could also be achieved with side effects. Box could have a useEffect
that performs operations against the threejs instance.
Possibilities / assumptions
So why choose a custom renderer? I believe it may be zero, one, or more of the following:
- There is some performance gain by bypassing lifecycle/side effects?
- Tighter control over sequencing and mounting/unmounting?
- Simplicity of code, after the initial hurdle of setting up a custom renderer, the rest of the mappings become easier?