React and Where Things End up in the DOM
Leaving the topic of refs, there is one other important React feature that can help with influencing (indirect) DOM interaction: Portals.
When building user interfaces, you sometimes need to display elements and content conditionally. This was already covered in Chapter 5, Rendering Lists and Conditional Content. When rendering conditional content, React will inject that content into the place in the DOM where the overall component (in which the conditional content is defined) is located.
For example, when showing a conditional error message below an input field, that error message is right below the input in the DOM:
This behavior makes sense. Indeed, it would be pretty irritating if React were to start inserting DOM elements in random places. But in some scenarios, you may prefer a (conditional) DOM element to be inserted...