File tree 7 files changed +31
-26
lines changed
05.portals/01.problem.create
06.layout-computation/01.problem.layout-effect
07.imperative-handle/01.problem.ref
09.sync-external/03.problem.ssr
7 files changed +31
-26
lines changed Original file line number Diff line number Diff line change 1
1
# Context Provider
2
2
3
- π§ββοΈ I was refactoring things a bit and decided we shouldn't need to pass
4
- the ` query ` and ` setSearchParams ` as props. Instead I just wanted to call
5
- ` useSearchParams ` in each of the components that need them (there's only one
6
- URL afterall).
3
+ π§ββοΈ I was < PrevDiffLink > refactoring things</ PrevDiffLink > a bit and decided we
4
+ shouldn't need to pass the ` query ` and ` setSearchParams ` as props. Instead I
5
+ just wanted to call ` useSearchParams ` in each of the components that need them
6
+ (there's only one URL afterall).
7
7
8
8
Well that didn't work. Turns out we now have multiple instances of the search
9
9
params state and multiple subscriptions to the ` popstate ` event. That's not
Original file line number Diff line number Diff line change 1
1
# Context Hook
2
2
3
3
π§ββοΈ I was playing around with the app and I realized that we have a default value
4
- for our context provider. So I thought maybe we could remove the context
5
- provider. Unfortunately that didn't work. Do you know why?
4
+ for our context provider. So I thought maybe we
5
+ could <PrevDiffLink >remove the context</PrevDiffLink > provider. Unfortunately
6
+ that didn't work. Do you know why?
6
7
7
8
π¨βπΌ Yeah, it's because even though the ` searchParams ` are shared, they're not
8
9
updated when calling ` setSearchParams ` . This is because the ` searchParams `
Original file line number Diff line number Diff line change 3
3
π¨βπΌ Some of our users find the heart icon to be unclear and would like to have a
4
4
tooltip that explains what it's for.
5
5
6
- π§ββοΈ I've moved things around a little bit to reduce the amount of code you need
7
- to work in. I've also added a simple tooltip component that's not working quite
8
- yet. The positioning is all funny because the tooltip is being rendered within
9
- the context of the card instead of at the root of the document.
6
+ π§ββοΈ I've <PrevDiffLink >moved things around a little bit</PrevDiffLink > to reduce
7
+ the amount of code you need to work in. I've also added a simple tooltip
8
+ component that's not working quite yet. The positioning is all funny because the
9
+ tooltip is being rendered within the context of the card instead of at the root
10
+ of the document.
10
11
11
12
π¨βπΌ Thanks Kellie. Now, let's see if you can make the tooltip component work
12
13
properly with a portal.
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ That `height` is used to determine whether the tooltip should appear above or
16
16
below the target element (the heart in our case).
17
17
18
18
Kellie π§ββοΈ noticed on low-end devices, they're seeing a little flicker
19
- so <DiffLink app1 = { - 1 } >she's added</DiffLink > an arbitrary slowdown to our
19
+ so <PrevDiffLink >she's added</PrevDiffLink > an arbitrary slowdown to our
20
20
component to simulate that problem. To reproduce the problem, simply hover over
21
21
a heart and you'll notice it starts at the bottom of the heart and then flickers
22
22
to the top (if there's room on the top of the heart).
@@ -25,3 +25,6 @@ So your job is simple. Change `useEffect` to `useLayoutEffect` and that should
25
25
fix things.
26
26
27
27
π Parts of this exercise was lifted from [ the React docs] ( https://react.dev/reference/react/useLayoutEffect#measuring-layout-before-the-browser-repaints-the-screen )
28
+
29
+ π Learn more about the difference between ` useEffect ` and ` useLayoutEffect ` in
30
+ [ useEffect vs useLayoutEffect] ( https://kentcdodds.com/blog/useeffect-vs-uselayouteffect ) .
Original file line number Diff line number Diff line change 1
1
# useImperativeHandle
2
2
3
- π¨βπΌ We've got a new thing for you to work on. Kellie π§ββοΈ put together a
4
- ` Scrollable ` component which is a wrapper around a ` div ` that has a way to
5
- scroll to the top and bottom of the content. We want to be able to add buttons
6
- to the ` App ` that will allow users to scroll to the top and bottom of the
7
- content when clicked.
3
+ π¨βπΌ We've got a new thing for you to work on.
8
4
9
- So we need you to ` useImperativeHandle ` to expose a ` scrollToTop ` and
5
+ π§ββοΈ I've put together a ` Scrollable ` component which is a wrapper around a ` div `
6
+ that has a way to scroll to the top and bottom of the content. We want to be
7
+ able to add buttons to the ` App ` that will allow users to scroll to the top and
8
+ bottom of the content when clicked.
9
+
10
+ π¨βπΌ So we need you to ` useImperativeHandle ` to expose a ` scrollToTop ` and
10
11
` scrollToBottom ` method from the ` Scrollable ` component. These methods are
11
12
already implemented, you just need to expose them.
Original file line number Diff line number Diff line change @@ -10,13 +10,12 @@ update. For example:
10
10
11
11
``` tsx
12
12
function MyComponent() {
13
- const inputRef = useRef <HTMLInputElement >(null )
14
13
const [show, setShow] = useState (false )
15
14
16
15
return (
17
16
<div >
18
17
<button onClick = { () => setShow (true )} >Show</button >
19
- { show && <input ref = { inputRef } /> }
18
+ { show ? <input /> : null }
20
19
</div >
21
20
)
22
21
}
@@ -49,7 +48,7 @@ function MyComponent() {
49
48
>
50
49
Show
51
50
</button >
52
- { show && <input ref = { inputRef } />}
51
+ { show ? <input ref = { inputRef } /> : null }
53
52
</div >
54
53
)
55
54
}
@@ -80,7 +79,7 @@ function MyComponent() {
80
79
>
81
80
Show
82
81
</button >
83
- { show && <input ref = { inputRef } />}
82
+ { show ? <input ref = { inputRef } /> : null }
84
83
</div >
85
84
)
86
85
}
@@ -93,4 +92,4 @@ to focus it on the line following the `flushSync` call.
93
92
In general you want to avoid this de-optimization, but in some cases (like focus
94
93
management), it's the perfect solution.
95
94
96
- Learn more in [ the ` flushSync ` docs] ( https://react.dev/reference/react-dom/flushSync ) .
95
+ Learn more in [ π the ` flushSync ` docs] ( https://react.dev/reference/react-dom/flushSync ) .
Original file line number Diff line number Diff line change 3
3
π¨βπΌ We don't currently do any server rendering, but in the future we may want to
4
4
and this requires some special handling with ` useSyncExternalStore ` .
5
5
6
- π§ββοΈ I've simulated a server rendering environment by adding some code to the
7
- bottom of our file. First, we render the ` <App /> ` to a string, then we set that
8
- to the ` innerHTML ` of our ` rootEl ` . Then we call ` hydrateRoot ` to rehydrate our
9
- application.
6
+ π§ββοΈ < PrevDiffLink > I've simulated a server rendering environment</ PrevDiffLink > by
7
+ adding some code to the bottom of our file. First, we render the ` <App /> ` to a
8
+ string, then we set that to the ` innerHTML ` of our ` rootEl ` . Then we call
9
+ ` hydrateRoot ` to rehydrate our application.
10
10
11
11
π¨βπΌ This is a bit of a hack, but it's a good way to simulate server rendering
12
12
and ensure that our application works in a server rendering situation.
You canβt perform that action at this time.
0 commit comments