-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add RFC-229 based way with use and Suspense for data fetching (#62)
Co-authored-by: lowsky <lowsky@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
132 additions
and
21 deletions.
There are no files selected for viewing
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
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
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,69 @@ | ||
'use client'; | ||
import React, { use, Suspense, cache } from 'react'; | ||
|
||
import { fetchRepoBranchesWithCommitStatusesAndPullRequests, fetchUser, User } from '../../../restinpeace/github'; | ||
import { UserRepoFromUrlProvider, useUserRepo } from '../../../components/useUserRepoFromRoute'; | ||
import UserRepo from '../../../container/UserRepo'; | ||
import RichErrorBoundary from '../../../components/RichErrorBoundary'; | ||
import InternalLink from '../../../components/InternalLink'; | ||
import { Spinner } from '../../../components/Spinner'; | ||
import { RepoType } from '../../../components/Repo'; | ||
|
||
function delay(timeout) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, timeout); | ||
}); | ||
} | ||
|
||
export default function ReactNextDrivenPage() { | ||
return ( | ||
<UserRepoFromUrlProvider> | ||
<ReactNextWithUrlContext /> | ||
</UserRepoFromUrlProvider> | ||
); | ||
} | ||
|
||
function ReactNextWithUrlContext() { | ||
const { userName, repoName } = useUserRepo(); | ||
const userData: Promise<User> = fetchUserPromise(userName); | ||
const repoData: Promise<RepoType> = fetchRepoBranches({ userName, repoName }); | ||
return ( | ||
<UserRepoFromUrlProvider> | ||
<RichErrorBoundary> | ||
<InternalLink href={'/next'}>back to repos</InternalLink> | ||
<Suspense fallback={<Spinner />}> | ||
<ReactNext userData={userData} repoData={repoData} /> | ||
</Suspense> | ||
</RichErrorBoundary> | ||
</UserRepoFromUrlProvider> | ||
); | ||
} | ||
|
||
const fetchUserPromise: (id) => Promise<User> = cache(async (id) => { | ||
await delay(2000); | ||
return fetchUser(id); | ||
}); | ||
const fetchRepoBranches: ({ userName, repoName }) => Promise<RepoType> = cache(async ({ userName, repoName }) => { | ||
await delay(2000); | ||
return fetchRepoBranchesWithCommitStatusesAndPullRequestsProm({ userName, repoName }); | ||
}); | ||
|
||
interface Props { | ||
userData: Promise<User>; | ||
repoData: Promise<RepoType>; | ||
} | ||
|
||
function ReactNext({ userData, repoData }: Props) { | ||
const user = use(userData); | ||
const repo: RepoType = use(repoData); | ||
|
||
return <UserRepo user={user} repo={repo} />; | ||
} | ||
|
||
const fetchRepoBranchesWithCommitStatusesAndPullRequestsProm = cache(async ({ userName, repoName }) => | ||
fetchRepoBranchesWithCommitStatusesAndPullRequests({ userName, repoName }).then((branchesWithCommit) => ({ | ||
name: repoName, | ||
owner: { login: userName }, | ||
branches: branchesWithCommit.branches, | ||
})) | ||
); |
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,27 @@ | ||
'use client'; // this directive should be at top of the file, before any imports. | ||
|
||
import React from 'react'; | ||
import { Box, Heading, Link } from '@chakra-ui/react'; | ||
|
||
import InternalLink from '../../components/InternalLink'; | ||
import { LinkList } from '../../components/LinkList'; | ||
|
||
export default function Shortcuts() { | ||
return ( | ||
<div> | ||
<InternalLink href={'/'}>back to main page</InternalLink> | ||
|
||
<Heading>Experimental, RFC-229</Heading> | ||
<Box mb={6}> | ||
<strong>use(await fetch)</strong> Fetching all data and use latest support of async data fetching via | ||
`const data = use(await fetch)` | ||
</Box> | ||
<Box mb={6}> | ||
See <Link href="https://github.com/reactjs/rfcs/pull/229">GH/reactjs/rfcs/pull/229</Link> for mor | ||
details | ||
</Box> | ||
|
||
<LinkList rootPath="/next" /> | ||
</div> | ||
); | ||
} |
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
7e8ebb5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-suspense-meetup-demo – ./
react-suspense-meetup-demo-lowsky.vercel.app
react-suspense-meetup-demo-git-main-lowsky.vercel.app
react-suspense-meetup-demo.vercel.app