-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f456fd7
commit e06dd63
Showing
10 changed files
with
127 additions
and
117 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
exercises/09.sync-external/02.solution/narrow-media-query.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Make Store Utility |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useSyncExternalStore } from 'react' | ||
import * as ReactDOM from 'react-dom/client' | ||
|
||
export function makeMediaQueryStore(mediaQuery: string) { | ||
function getSnapshot() { | ||
return window.matchMedia(mediaQuery).matches | ||
} | ||
|
||
function subscribe(callback: () => void) { | ||
const mediaQueryList = window.matchMedia(mediaQuery) | ||
mediaQueryList.addEventListener('change', callback) | ||
return () => { | ||
mediaQueryList.removeEventListener('change', callback) | ||
} | ||
} | ||
|
||
return function useMediaQuery() { | ||
return useSyncExternalStore(subscribe, getSnapshot) | ||
} | ||
} | ||
|
||
const useNarrowMediaQuery = makeMediaQueryStore('(max-width: 600px)') | ||
|
||
function NarrowScreenNotifier() { | ||
const isNarrow = useNarrowMediaQuery() | ||
return isNarrow ? 'You are on a narrow screen' : 'You are on a wide screen' | ||
} | ||
|
||
function App() { | ||
return ( | ||
<div> | ||
<div>This is your narrow screen state:</div> | ||
{/* 🐨 add a Suspense component around this with a fallback prop */} | ||
<NarrowScreenNotifier /> | ||
</div> | ||
) | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
// 🦉 here's how we pretend we're server-rendering | ||
rootEl.innerHTML = (await import('react-dom/server')).renderToString(<App />) | ||
|
||
// 🦉 here's how we simulate a delay in hydrating with client-side js | ||
await new Promise(resolve => setTimeout(resolve, 1000)) | ||
|
||
ReactDOM.hydrateRoot(rootEl, <App />, { | ||
// 💯 if you want to silence the error add a onRecoverableError function here | ||
// and if the error includes 'Missing getServerSnapshot' then return early | ||
// otherwise log the error so you don't miss any other errors. | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Make Store Utility |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Suspense, useSyncExternalStore } from 'react' | ||
import * as ReactDOM from 'react-dom/client' | ||
|
||
export function makeMediaQueryStore(mediaQuery: string) { | ||
function getSnapshot() { | ||
return window.matchMedia(mediaQuery).matches | ||
} | ||
|
||
function subscribe(callback: () => void) { | ||
const mediaQueryList = window.matchMedia(mediaQuery) | ||
mediaQueryList.addEventListener('change', callback) | ||
return () => { | ||
mediaQueryList.removeEventListener('change', callback) | ||
} | ||
} | ||
|
||
return function useMediaQuery() { | ||
return useSyncExternalStore(subscribe, getSnapshot) | ||
} | ||
} | ||
|
||
const useNarrowMediaQuery = makeMediaQueryStore('(max-width: 600px)') | ||
|
||
function NarrowScreenNotifier() { | ||
const isNarrow = useNarrowMediaQuery() | ||
return isNarrow ? 'You are on a narrow screen' : 'You are on a wide screen' | ||
} | ||
|
||
function App() { | ||
return ( | ||
<div> | ||
<div>This is your narrow screen state:</div> | ||
<Suspense fallback=""> | ||
<NarrowScreenNotifier /> | ||
</Suspense> | ||
</div> | ||
) | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
// 🦉 here's how we pretend we're server-rendering | ||
rootEl.innerHTML = (await import('react-dom/server')).renderToString(<App />) | ||
|
||
// 🦉 here's how we simulate a delay in hydrating with client-side js | ||
await new Promise(resolve => setTimeout(resolve, 1000)) | ||
|
||
ReactDOM.hydrateRoot(rootEl, <App />, { | ||
onRecoverableError(error) { | ||
if (String(error).includes('Missing getServerSnapshot')) return | ||
|
||
console.error(error) | ||
}, | ||
}) |