mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-06 07:21:16 +08:00
55 lines
1.3 KiB
Plaintext
55 lines
1.3 KiB
Plaintext
```js
|
|
// YourPage.js|jsx|ts|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('idle');
|
|
const [data, setData] = useState([]);
|
|
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>
|
|
);
|
|
}
|
|
``` |