MCPcopy
hub / github.com/epicweb-dev/react-hooks / App

Function App

exercises/02.side-effects/02.solution.cleanup/index.tsx:11–92  Β·  view source on GitHub β†—
()

Source from the content-addressed store, hash-verified

9}
10
11function App() {
12 const [query, setQuery] = useState(getQueryParam)
13
14 const words = query.split(' ')
15
16 const dogChecked = words.includes('dog')
17 const catChecked = words.includes('cat')
18 const caterpillarChecked = words.includes('caterpillar')
19
20 useEffect(() => {
21 // 🚨 we use this to test whether your cleanup is working
22 const hugeData = new Array(1_000_000).fill(
23 new Array(1_000_000).fill('πŸΆπŸ±πŸ›'),
24 )
25 function updateQuery() {
26 // 🚨 this console.log forces the hugeData to hang around as long as the event listener is active
27 console.log(hugeData)
28 console.log('popstate event listener called')
29 setQuery(getQueryParam())
30 }
31 window.addEventListener('popstate', updateQuery)
32 return () => {
33 window.removeEventListener('popstate', updateQuery)
34 }
35 }, [])
36
37 function handleCheck(tag: string, checked: boolean) {
38 const newWords = checked ? [...words, tag] : words.filter((w) => w !== tag)
39 setQuery(newWords.filter(Boolean).join(' ').trim())
40 }
41
42 return (
43 <div className="app">
44 <form
45 action={() => {
46 setGlobalSearchParams({ query })
47 }}
48 >
49 <div>
50 <label htmlFor="searchInput">Search:</label>
51 <input
52 id="searchInput"
53 name="query"
54 type="search"
55 value={query}
56 onChange={(e) => setQuery(e.currentTarget.value)}
57 />
58 </div>
59 <div>
60 <label>
61 <input
62 type="checkbox"
63 checked={dogChecked}
64 onChange={(e) => handleCheck('dog', e.currentTarget.checked)}
65 />{' '}
66 🐢 dog
67 </label>
68 <label>

Callers

nothing calls this directly

Calls 2

setGlobalSearchParamsFunction Β· 0.90
handleCheckFunction Β· 0.70

Tested by

no test coverage detected