-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new snippet to CodeSnippets paths
- Loading branch information
1 parent
50f3fdb
commit 81611b9
Showing
2 changed files
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
```ts | ||
// YourPage.tsx | ||
|
||
import React, { useState, useEffect } from 'react'; | ||
|
||
import { PageLayout } from './PageLayout'; | ||
import { DocumentHeader } from './DocumentHeader'; | ||
import { DocumentList } from './DocumentList'; | ||
|
||
// Example hook to retrieve data from an external endpoint | ||
function useFetchData() { | ||
const [status, setStatus] = useState<string>('idle'); | ||
const [data, setData] = useState<any[]>([]); | ||
useEffect(() => { | ||
setStatus('loading'); | ||
fetch('https://your-restful-endpoint') | ||
.then((res) => { | ||
if (!res.ok) { | ||
throw new Error(res.statusText); | ||
} | ||
return res; | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
setStatus('success'); | ||
setData(data); | ||
}) | ||
.catch(() => { | ||
setStatus('error'); | ||
}); | ||
}, []); | ||
|
||
return { | ||
status, | ||
data, | ||
}; | ||
} | ||
|
||
export function DocumentScreen() { | ||
const { status, data } = useFetchData(); | ||
|
||
const { user, document, subdocuments } = data; | ||
|
||
if (status === 'loading') { | ||
return <p>Loading...</p>; | ||
} | ||
if (status === 'error') { | ||
return <p>There was an error fetching the data!</p>; | ||
} | ||
return ( | ||
<PageLayout user={user}> | ||
<DocumentHeader document={document} /> | ||
<DocumentList documents={subdocuments} /> | ||
</PageLayout> | ||
); | ||
} |
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