Skip to content

Commit e26e676

Browse files
authored
Suggest alternative for state change
Using setState inherently causes another re-render on change, even if according to the value nothing changed. useRef would be more accurate
1 parent 2da4f7f commit e26e676

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/content/learn/you-might-not-need-an-effect.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ function List({ items }) {
231231
const [selection, setSelection] = useState(null);
232232

233233
// Better: Adjust the state while rendering
234-
const [prevItems, setPrevItems] = useState(items);
235-
if (items !== prevItems) {
236-
setPrevItems(items);
234+
const prevItems = useRef(items);
235+
if (items !== prevItems.current) {
236+
prevItems.current = items;
237237
setSelection(null);
238238
}
239239
// ...

0 commit comments

Comments
 (0)