Skip to content

Commit

Permalink
feat: add new snippet to CodeSnippets paths
Browse files Browse the repository at this point in the history
  • Loading branch information
halitiince authored Oct 20, 2023
1 parent 50f3fdb commit 81611b9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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

0 comments on commit 81611b9

Please sign in to comment.