Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: add new snippet to CodeSnippets paths #24540

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/snippets/react/document-screen-fetch-ts.mdx
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>
);
}
1 change: 1 addition & 0 deletions docs/writing-stories/build-pages-with-storybook.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ If you're working with pure presentational screens, adding stories through [args
<CodeSnippets
paths={[
'react/document-screen-fetch.js.mdx',
'react/document-screen-fetch.ts.mdx',
'vue/document-screen-fetch.3.js.mdx',
'vue/document-screen-fetch.3.ts.mdx',
'angular/document-screen-fetch.ts.mdx',
Expand Down