|
1 | 1 | # DOM Side-Effects
|
2 | 2 |
|
3 |
| -## 📝 Your Notes |
4 |
| - |
5 |
| -Elaborate on your learnings here in `src/exercise/05.md` |
6 |
| - |
7 |
| -## Background |
8 |
| - |
9 | 3 | Often when working with React you'll need to interact with the DOM directly. You
|
10 | 4 | may need to use a vanilla-js (non-framework-specific) library that needs to
|
11 | 5 | interact with directly with the DOM. Often to make a UI accessible you need to
|
@@ -69,57 +63,3 @@ state if you're unsure and then move to a ref if you decide you don't want a
|
69 | 63 | rerender when it's updated. We'll dive deeper into non-DOM refs in future
|
70 | 64 | workshops.
|
71 | 65 |
|
72 |
| -## Exercise |
73 |
| - |
74 |
| -Production deploys: |
75 |
| - |
76 |
| -- [Exercise](https://react-hooks-next.netlify.app/isolated/exercise/05.tsx) |
77 |
| -- [Final](https://react-hooks-next.netlify.app/isolated/final/05.tsx) |
78 |
| - |
79 |
| -For this extra credit we're going to auto-focus on the username input if there's |
80 |
| -an error after submitting. This is a common practice for accessibility purposes |
81 |
| -(to focus on the first input that has an error after a form submission). |
82 |
| - |
83 |
| -So for this exercise you're going to use `useRef`. The example above should be |
84 |
| -enough to get you working. You'll pass the ref you create to the input and then |
85 |
| -in a `useEffect` you'll call `.focus()` on the input element whenever the error |
86 |
| -is displayed. |
87 |
| - |
88 |
| -## Extra Credit |
89 |
| - |
90 |
| -### 1. 💯 Side-effect cleanup |
91 |
| - |
92 |
| -In this extra credit we're going to use a completely different example. We're |
93 |
| -going to make a `<Tilt />` component that renders a div and uses the |
94 |
| -`vanilla-tilt` library to make it super fancy. |
95 |
| - |
96 |
| -The thing is, `vanilla-tilt` works directly with DOM nodes to setup event |
97 |
| -handlers and stuff, so we need access to the DOM node. But because we're not the |
98 |
| -one calling `document.createElement` (React does) we need React to give it to |
99 |
| -us. |
100 |
| - |
101 |
| -So in this exercise we're going to use a `ref` so React can give us the DOM node |
102 |
| -and then we can pass that on to `vanilla-tilt`. |
103 |
| - |
104 |
| -Additionally, we'll need to clean up after ourselves if this component is |
105 |
| -unmounted. Otherwise we'll have event handlers dangling around on DOM nodes that |
106 |
| -are no longer in the document which can cause a memory leak. |
107 |
| - |
108 |
| -To be clear about the memory leak, just imagine what would happen if we mount a |
109 |
| -tilt element, then unmount it (without cleaning up). Those DOM nodes hang around |
110 |
| -because the event listeners are still listening to events on them (even though |
111 |
| -they're no longer in the document). Then let's say we mount a new one and |
112 |
| -unmount that again. Now we have two sets of DOM nodes that are still in memory, |
113 |
| -but not being used. We keep doing this over and over again and eventually our |
114 |
| -computer is just keeping track of all these DOM nodes we don't need it to. |
115 |
| -That's what's called a memory leak. So it's really important we clean up after |
116 |
| -ourselves to avoid the performance problems associated with memory leaks. |
117 |
| - |
118 |
| -NOTE: Because this extra credit is a completely different example, you've got |
119 |
| -another starting point file in: `src/exercise/05.extra-1.tsx`. 🐨 and 💰 will be |
120 |
| -there to help you get that working. |
121 |
| - |
122 |
| -## 🦉 Feedback |
123 |
| - |
124 |
| -Fill out |
125 |
| -[the feedback form](https://ws.kcd.im/?ws=React%20Hooks%20%F0%9F%8E%A3&e=05%3A%20useRef%20and%20useEffect%3A%20DOM%20interaction&em=). |
0 commit comments