You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/2024-09-28-modernizing-react.md
+14-4Lines changed: 14 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,7 @@ Now that we've done the easy part, the next step is to convert all the functions
60
60
61
61
### Reactive Members to Hooks
62
62
63
-
I've found it easiest to start with picking out the variables and functions that are used for reactivity, because these are the things that will go into one or more `useEffect()` functions. This includes legacy lifecycle hooks like `componentDidMount()`, which have been deprecated.
63
+
I've found it easiest to start with picking out the variables and functions that are used for reactivity, because these are the things that will go into one or more `useEffect()`(or `useMemo` or `useState`, or...) functions. This includes legacy lifecycle hooks like `componentDidMount()`, which have been deprecated.
64
64
65
65
For a simple example, let's say we have something like this:
66
66
@@ -88,13 +88,20 @@ That might become:
88
88
const [widgets, setWidgets] =useState({});
89
89
const [widgetCount, setWidgetCount] =useState(0);
90
90
91
-
// Passing an empty array as the second parameter runs the body on load
92
-
// You don't want to set them in useState, because that's run on every repaint
91
+
// Passing an empty array as the second parameter runs the body on load.
92
+
// You don't want to set them in useState, because that's run on every repaint.
93
+
// Since we're loading external data, this is how it's supposed to be done.
93
94
useEffect(() => {
94
95
setWidgets =fetchWidgets();
95
96
}, []);
96
97
97
98
useEffect(() => {
99
+
// This is technically frowned upon, but I found this to misbehave on repaint
100
+
// at the time that I did this refactor when doing it the recommended way and
101
+
// only worked properly when done this way.
102
+
// Additionally, when we refactor a legacy system, we might not know where else
103
+
// "widgets" gets changed. Once we go through and identify those change points,
@@ -104,9 +111,12 @@ How `useEffect` and some of the other newer hooks work can take a little getting
104
111
105
112
This is a very simple example, so the gains aren't as prominent, but I've reduced the size of some large components by as much as 60%, almost solely from this step. More importantly, though, is that I've greatly reduced the *complexity* of those components.
106
113
114
+
> [!important]
115
+
> [The React docs](https://react.dev/learn/you-might-not-need-an-effect) have an in-depth post on when to use `useEffect`, specifically, and when to use other techniques. It's worth the read to gain some understanding. I've found when doing these refactorings, however, that sometimes `useEffect` is needed more often for an interim period to maintain the original behavior (due to how the old lifecycle hooks map to the new structure), until a larger refactor of the underlying behavior as a whole is done. It's definitely a good idea to make note when you find yourself needing `useEffect` in a less than ideal way, so you can revisit it later. Remember - we're not looking for perfection of the gate here, user experience stability reins supreme.
116
+
107
117
### Functions To Arrow Function Constants
108
118
109
-
While there are some cases where the old function syntax is still needed, and this part isn't *strictuly* necessary, the vast majority of them can be converted to arrow functions.
119
+
While there are some cases where the old function syntax is still needed, and this part isn't *strictly* necessary, the vast majority of them can be converted to arrow functions.
0 commit comments