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

Function App

exercises/02.side-effects/01.problem.effects/index.tsx:11โ€“81  ยท  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 // ๐Ÿจ add a useEffect(() => {}, []) call here (we'll talk about that empty array later)
21 // ๐Ÿจ in the useEffect callback, subscribe to window's popstate event
22 // ๐Ÿฆ‰ if that doesn't make sense to you... don't worry, it's supposed to be broken! We'll fix it next
23 // ๐Ÿจ your event handler should call setQuery to getQueryParam()
24 // ๐Ÿ“œ https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
25
26 function handleCheck(tag: string, checked: boolean) {
27 const newWords = checked ? [...words, tag] : words.filter((w) => w !== tag)
28 setQuery(newWords.filter(Boolean).join(' ').trim())
29 }
30
31 return (
32 <div className="app">
33 <form
34 action={() => {
35 setGlobalSearchParams({ query })
36 }}
37 >
38 <div>
39 <label htmlFor="searchInput">Search:</label>
40 <input
41 id="searchInput"
42 name="query"
43 type="search"
44 value={query}
45 onChange={(e) => setQuery(e.currentTarget.value)}
46 />
47 </div>
48 <div>
49 <label>
50 <input
51 type="checkbox"
52 checked={dogChecked}
53 onChange={(e) => handleCheck('dog', e.currentTarget.checked)}
54 />{' '}
55 ๐Ÿถ dog
56 </label>
57 <label>
58 <input
59 type="checkbox"
60 checked={catChecked}
61 onChange={(e) => handleCheck('cat', e.currentTarget.checked)}
62 />{' '}
63 ๐Ÿฑ cat
64 </label>
65 <label>
66 <input
67 type="checkbox"
68 checked={caterpillarChecked}

Callers

nothing calls this directly

Calls 2

setGlobalSearchParamsFunction ยท 0.90
handleCheckFunction ยท 0.70

Tested by

no test coverage detected