Skip to content

Commit

Permalink
docs: async hooks docs and stories update
Browse files Browse the repository at this point in the history
  • Loading branch information
wardoost committed Jul 16, 2019
1 parent 1c28c70 commit 9ea0275
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 69 deletions.
21 changes: 11 additions & 10 deletions docs/useAsync.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ a promise;
```jsx
import {useAsync} from 'react-use';

const Demo = ({delay = 1000}) => {
const state = useAsync(() => {
// Returns a Promise that resolves after x milliseconds
return new Promise((resolve) => setTimeout(() => resolve('RESOLVED'), delay);
}, [delay]);
const Demo = ({url}) => {
const state = useAsync(async () => {
const response = await fetch(url);
const result = await response.text();
return result
}, [url]);

return (
<div>
{state.loading?
<div>Loading...</div>
: state.error?
<div>Error...</div>
: <div>Value: {state.value}</div>
{state.loading
? <div>Loading...</div>
: state.error
? <div>Error: {state.error.message}</div>
: <div>Value: {state.value}</div>
}
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions docs/useAsyncFn.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function that returns a promise. The state is of the same shape as `useAsync`.
```jsx
import {useAsyncFn} from 'react-use';

const Demo = (url) => {
const Demo = ({url}) => {
const [state, fetch] = useAsyncFn(async () => {
const response = await fetch(url);
const result = await response.text();
Expand All @@ -21,7 +21,7 @@ const Demo = (url) => {
? <div>Loading...</div>
: state.error
? <div>Error: {state.error.message}</div>
: state.value && <div>Value: {state.value}</div>
: <div>Value: {state.value}</div>
}
<button onClick={() => fetch()}>Start loading</button>
</div>
Expand Down
36 changes: 12 additions & 24 deletions docs/useAsyncRetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,32 @@

Uses `useAsync` with an additional `retry` method to easily retry/refresh the async function;


## Usage

```jsx
import {useAsyncRetry} from 'react-use';

// Returns a Promise that resolves after one second.
const fn = () => new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
reject(new Error('Random error!'));
} else {
resolve('RESOLVED');
}
}, 1000);
});

const Demo = () => {
const state = useAsyncRetry(fn);
const Demo = ({url}) => {
const state = useAsyncRetry(async () => {
const response = await fetch(url);
const result = await response.text();
return result;
}, [url]);

return (
<div>
{state.loading?
<div>Loading...</div>
: state.error?
<div>Error...</div>
: <div>Value: {state.value}</div>
}
{!state.loading?
<a href='javascript:void 0' onClick={() => state.retry()}>Retry</a>
: null
{state.loading
? <div>Loading...</div>
: state.error
? <div>Error: {state.error.message}</div>
: <div>Value: {state.value}</div>
}
{!loading && <button onClick={() => state.retry()}>Start loading</button>}
</div>
);
};
```


## Reference

```ts
Expand Down
23 changes: 19 additions & 4 deletions src/__stories__/useAsync.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,31 @@ import { useAsync } from '..';
import ShowDocs from './util/ShowDocs';

const Demo = ({ delay }) => {
const { loading, error, value } = useAsync<string>(
const state = useAsync<string>(
() =>
new Promise<string>(resolve => {
setTimeout(() => resolve('RESOLVED'), delay);
new Promise<string>((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
resolve('✌️');
} else {
reject(new Error('A pseudo random error occurred'));
}
}, delay);
}),
[delay]
);

return (
<div>{loading ? <div>Loading...</div> : error ? <div>Error: {error.message}</div> : <div>Value: {value}</div>}</div>
<div>
{state.loading ? (
<p>Loading...</p>
) : state.error ? (
<p>Error: {state.error.message}</p>
) : (
<p>Value: {state.value}</p>
)}
<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
);
};

Expand Down
33 changes: 20 additions & 13 deletions src/__stories__/useAsyncFn.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@ import * as React from 'react';
import { useAsyncFn } from '..';
import ShowDocs from './util/ShowDocs';

const fn = () =>
new Promise<string>((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
reject(new Error('Random error!'));
} else {
resolve('RESOLVED');
}
}, 1000);
});

const Demo = () => {
const [{ loading, error, value }, callback] = useAsyncFn<string>(fn);
const [state, callback] = useAsyncFn<string>(
() =>
new Promise<string>((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
resolve('✌️');
} else {
reject(new Error('A pseudo random error occurred'));
}
}, 1000);
})
);

return (
<div>
{loading ? <div>Loading...</div> : error ? <div>Error: {error.message}</div> : value && <div>Value: {value}</div>}
{state.loading ? (
<p>Loading...</p>
) : state.error ? (
<p>Error: {state.error.message}</p>
) : (
<p>Value: {state.value}</p>
)}
<button onClick={() => callback()}>Start</button>
<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
);
};
Expand Down
45 changes: 29 additions & 16 deletions src/__stories__/useAsyncRetry.story.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
import { number, withKnobs } from '@storybook/addon-knobs';
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useAsyncRetry } from '..';
import ShowDocs from './util/ShowDocs';

const fnRetry = () =>
new Promise<string>((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
reject(new Error('Random error!'));
} else {
resolve('RESOLVED');
}
}, 1000);
});

const DemoRetry = () => {
const { loading, value, error, retry } = useAsyncRetry<string>(fnRetry);
const Demo = ({ delay }) => {
const state = useAsyncRetry<string>(
() =>
new Promise<string>((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
resolve('✌️');
} else {
reject(new Error('A pseudo random error occurred'));
}
}, delay);
}),
[delay]
);

return (
<div>
{loading ? <div>Loading...</div> : error ? <div>Error: {error.message}</div> : <div>Value: {value}</div>}
<button onClick={() => retry()}>Retry</button>
{state.loading ? (
<p>Loading...</p>
) : state.error ? (
<p>Error: {state.error.message}</p>
) : (
<p>Value: {state.value}</p>
)}
<button onClick={() => state.retry()}>Retry</button>
<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
);
};

storiesOf('Side effects|useAsyncRetry', module)
.addDecorator(withKnobs)
.add('Docs', () => <ShowDocs md={require('../../docs/useAsyncRetry.md')} />)
.add('Demo', () => <DemoRetry />);
.add('Demo', () => {
const delay = number('delay', 1000, { range: true, min: 100, max: 5000, step: 100 });
return <Demo delay={delay} />;
});

0 comments on commit 9ea0275

Please sign in to comment.