()
| 9 | } |
| 10 | |
| 11 | function 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} |
nothing calls this directly
no test coverage detected