1+ diff --git a/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.solution/7h2jowvfi2q/index.test.tsx b/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.solution/7h2jowvfi2q/index.test.tsx
2+ new file mode 100644
3+ index 0000000..e69de29
4+ diff --git a/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.problem/7h2jowvfi2q/index.tsx b/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.solution/7h2jowvfi2q/index.tsx
5+ index 9913856..4d68325 100644
6+ --- a/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.problem/7h2jowvfi2q/index.tsx
7+ +++ b/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.solution/7h2jowvfi2q/index.tsx
8+ @@ -1,4 +1,4 @@
9+ -import { useCallback, useEffect, useState } from 'react'
10+ +import { createContext, useEffect, useState, use, useCallback } from 'react'
11+ import * as ReactDOM from 'react-dom/client'
12+ import {
13+ type BlogPost,
14+ @@ -7,15 +7,16 @@ import {
15+ } from '#shared/blog-posts'
16+ import { setGlobalSearchParams } from '#shared/utils'
17+
18+ -// 🦺 create a SearchParamsTuple type here that's a readonly array of two elements:
19+ -// - the first element is a URLSearchParams instance
20+ -// - the second element is typeof setGlobalSearchParams
21+ -// 🐨 create a SearchParamsContext that is of this type
22+ -// 💰 let's start with this as the default value (we'll improve it next):
23+ -// [new URLSearchParams(window.location.search), setGlobalSearchParams]
24+ +type SearchParamsTuple = readonly [
25+ + URLSearchParams,
26+ + typeof setGlobalSearchParams,
27+ +]
28+ +const SearchParamsContext = createContext<SearchParamsTuple>([
29+ + new URLSearchParams(window.location.search),
30+ + setGlobalSearchParams,
31+ +])
32+
33+ -// 🐨 change this to SearchParamsProvider and accept children
34+ -function useSearchParams() {
35+ +function SearchParamsProvider({ children }: { children: React.ReactNode }) {
36+ const [searchParams, setSearchParamsState] = useState(
37+ () => new URLSearchParams(window.location.search),
38+ )
39+ @@ -46,23 +47,29 @@ function useSearchParams() {
40+ [],
41+ )
42+
43+ - // 🐨 instead of returning this, render the SearchParamsContext and
44+ - // provide this tuple as the value
45+ - // 💰 make sure to render the children as well!
46+ - return [searchParams, setSearchParams] as const
47+ + const searchParamsTuple = [searchParams, setSearchParams] as const
48+ +
49+ + return (
50+ + <SearchParamsContext value={searchParamsTuple}>
51+ + {children}
52+ + </SearchParamsContext>
53+ + )
54+ }
55+
56+ -// 🐨 create a useSearchParams hook here that returns use(SearchParamsContext)
57+ +function useSearchParams() {
58+ + return use(SearchParamsContext)
59+ +}
60+
61+ const getQueryParam = (params: URLSearchParams) => params.get('query') ?? ''
62+
63+ function App() {
64+ return (
65+ - // 🐨 wrap this in the SearchParamsProvider
66+ - <div className="app">
67+ - <Form />
68+ - <MatchingPosts />
69+ - </div>
70+ + <SearchParamsProvider>
71+ + <div className="app">
72+ + <Form />
73+ + <MatchingPosts />
74+ + </div>
75+ + </SearchParamsProvider>
76+ )
77+ }
0 commit comments