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

Function App

exercises/02.side-effects/01.solution.effects/index.tsx:11–79  ·  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 window.addEventListener('popstate', () => setQuery(getQueryParam()))
22 }, [])
23
24 function handleCheck(tag: string, checked: boolean) {
25 const newWords = checked ? [...words, tag] : words.filter((w) => w !== tag)
26 setQuery(newWords.filter(Boolean).join(' ').trim())
27 }
28
29 return (
30 <div className="app">
31 <form
32 action={() => {
33 setGlobalSearchParams({ query })
34 }}
35 >
36 <div>
37 <label htmlFor="searchInput">Search:</label>
38 <input
39 id="searchInput"
40 name="query"
41 type="search"
42 value={query}
43 onChange={(e) => setQuery(e.currentTarget.value)}
44 />
45 </div>
46 <div>
47 <label>
48 <input
49 type="checkbox"
50 checked={dogChecked}
51 onChange={(e) => handleCheck('dog', e.currentTarget.checked)}
52 />{' '}
53 🐶 dog
54 </label>
55 <label>
56 <input
57 type="checkbox"
58 checked={catChecked}
59 onChange={(e) => handleCheck('cat', e.currentTarget.checked)}
60 />{' '}
61 🐱 cat
62 </label>
63 <label>
64 <input
65 type="checkbox"
66 checked={caterpillarChecked}
67 onChange={(e) =>
68 handleCheck('caterpillar', e.currentTarget.checked)

Callers

nothing calls this directly

Calls 3

setGlobalSearchParamsFunction · 0.90
getQueryParamFunction · 0.70
handleCheckFunction · 0.70

Tested by

no test coverage detected