()
| 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 | 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> |
nothing calls this directly
no test coverage detected