Direct DOM Access and Memory Leaks
Summary: Be able to use non-React libraries that directly interact with the DOM such as the VanillaTilt. Use
useRefto access dom nodes. Make sure that there are no memory leaks by cleaning up event handlers.
- Transform the perspective of a container on enter with this library
- Display the details of the perspective transform as it happens
- This exercise is a modified version of KCD's exercise
My Solution
Notes
DOM interactions
<div></div>is just a syntactic sugar forReact.createElement(), dom nodes are not created at all untilReactDom.render()is called.- The
rendermethod has no access to the dom node by itself, it only creates and returns react elements - To access the dom, use a special prop called
ref - A component that has rendered is said to be
mounted. That's whenuseEffectcallback is called. By that point,ref.currentis set to the dom node and you can directly do interactions and manipulations...
- ❗ ❗You CANNOT pass
refto a component as a prop the usual way that you might think - Ref forwarding is an opt-in feature that lets some components take a ref they receive, and pass it further down (in other words, “forward” it) to a child.
- ❗ ❗You CANNOT pass
Other interesting libraries that manipulate the dom
Further Reading