Skip to content

Commit

Permalink
Merge pull request #24540 from halitiince/halitiince/docs_update_new_…
Browse files Browse the repository at this point in the history
…react_ts_snippet

Docs: add new snippet to CodeSnippets paths
  • Loading branch information
jonniebigodes authored Oct 31, 2023
2 parents e3bc4e1 + 8d24485 commit d3fbed5
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 d3fbed5

Please sign in to comment.