Summary and Key Takeaways
- Refs can be used to gain direct access to DOM elements or to store values that won't be reset or changed when the surrounding component is re-evaluated.
- Only use this direct access to read values, not to manipulate DOM elements (let React do this instead).
- Components that gain DOM access via refs, instead of state and other React features, are considered uncontrolled components (because React is not in direct control).
- Prefer controlled components (using state and a strictly declarative approach) over uncontrolled components unless you're performing very simple tasks such as reading an entered input value.
- Using forward refs, you can also expose features of your own components such that they may be used imperatively.
- Portals can be used to instruct React to render JSX elements in a different place in the DOM than they normally would.
What's Next?
At this point in the book, you've encountered many...