Skip to content

Commit c63e131

Browse files
docs(start/tutorial): remove unnecessary useEffect (#9561)
Co-authored-by: Michaël De Boey <[email protected]>
1 parent 94aba39 commit c63e131

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@
505505
- n8agrin
506506
- na2hiro
507507
- nareshbhatia
508+
- nathggns
508509
- nauvalazhar
509510
- naveed-fida
510511
- navid-kalaei

docs/start/tutorial.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,22 +1210,25 @@ You could certainly do this as a controlled component. You will have more synchr
12101210
<summary>Expand this to see what it would look like</summary>
12111211

12121212
```tsx filename=app/root.tsx lines=[2,9-10,12-16,30-33,36-37]
1213-
// existing imports
1214-
import { useEffect, useState } from "react";
1213+
// We no longer need useEffect
1214+
import { useState } from "react";
12151215

12161216
// existing imports & exports
12171217

12181218
export default function App() {
12191219
const { contacts, q } = useLoaderData<typeof loader>();
12201220
const navigation = useNavigation();
12211221
// the query now needs to be kept in state
1222+
const [prevQ, setPrevQ] = useState(q);
12221223
const [query, setQuery] = useState(q || "");
12231224

1224-
// we still have a `useEffect` to synchronize the query
1225-
// to the component state on back/forward button clicks
1226-
useEffect(() => {
1225+
// We can avoid using `useEffect` to synchronize the query
1226+
// by using a separate piece of state to store the previous
1227+
// value
1228+
if (q !== prevQ) {
1229+
setPrevQ(q);
12271230
setQuery(q || "");
1228-
}, [q]);
1231+
}
12291232

12301233
return (
12311234
<html lang="en">

0 commit comments

Comments
 (0)