diff --git a/docs/useAsync.md b/docs/useAsync.md index 75c2a8c1cb..061f7ec85f 100644 --- a/docs/useAsync.md +++ b/docs/useAsync.md @@ -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 (
- {state.loading? -
Loading...
- : state.error? -
Error...
- :
Value: {state.value}
+ {state.loading + ?
Loading...
+ : state.error + ?
Error: {state.error.message}
+ :
Value: {state.value}
}
); diff --git a/docs/useAsyncFn.md b/docs/useAsyncFn.md index b3eabcdf9b..cc4825fb5a 100644 --- a/docs/useAsyncFn.md +++ b/docs/useAsyncFn.md @@ -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(); @@ -21,7 +21,7 @@ const Demo = (url) => { ?
Loading...
: state.error ?
Error: {state.error.message}
- : state.value &&
Value: {state.value}
+ :
Value: {state.value}
} diff --git a/docs/useAsyncRetry.md b/docs/useAsyncRetry.md index b31dee6a36..3b1dd5e6c6 100644 --- a/docs/useAsyncRetry.md +++ b/docs/useAsyncRetry.md @@ -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 (
- {state.loading? -
Loading...
- : state.error? -
Error...
- :
Value: {state.value}
- } - {!state.loading? - state.retry()}>Retry - : null + {state.loading + ?
Loading...
+ : state.error + ?
Error: {state.error.message}
+ :
Value: {state.value}
} + {!loading && }
); }; ``` - ## Reference ```ts diff --git a/src/__stories__/useAsync.story.tsx b/src/__stories__/useAsync.story.tsx index 2bc503f410..cc00e21832 100644 --- a/src/__stories__/useAsync.story.tsx +++ b/src/__stories__/useAsync.story.tsx @@ -5,16 +5,31 @@ import { useAsync } from '..'; import ShowDocs from './util/ShowDocs'; const Demo = ({ delay }) => { - const { loading, error, value } = useAsync( + const state = useAsync( () => - new Promise(resolve => { - setTimeout(() => resolve('RESOLVED'), delay); + new Promise((resolve, reject) => { + setTimeout(() => { + if (Math.random() > 0.5) { + resolve('✌️'); + } else { + reject(new Error('A pseudo random error occurred')); + } + }, delay); }), [delay] ); return ( -
{loading ?
Loading...
: error ?
Error: {error.message}
:
Value: {value}
}
+
+ {state.loading ? ( +

Loading...

+ ) : state.error ? ( +

Error: {state.error.message}

+ ) : ( +

Value: {state.value}

+ )} +
{JSON.stringify(state, null, 2)}
+
); }; diff --git a/src/__stories__/useAsyncFn.story.tsx b/src/__stories__/useAsyncFn.story.tsx index 75b9fda22c..eb630855ba 100644 --- a/src/__stories__/useAsyncFn.story.tsx +++ b/src/__stories__/useAsyncFn.story.tsx @@ -3,24 +3,31 @@ import * as React from 'react'; import { useAsyncFn } from '..'; import ShowDocs from './util/ShowDocs'; -const fn = () => - new Promise((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(fn); + const [state, callback] = useAsyncFn( + () => + new Promise((resolve, reject) => { + setTimeout(() => { + if (Math.random() > 0.5) { + resolve('✌️'); + } else { + reject(new Error('A pseudo random error occurred')); + } + }, 1000); + }) + ); return (
- {loading ?
Loading...
: error ?
Error: {error.message}
: value &&
Value: {value}
} + {state.loading ? ( +

Loading...

+ ) : state.error ? ( +

Error: {state.error.message}

+ ) : ( +

Value: {state.value}

+ )} +
{JSON.stringify(state, null, 2)}
); }; diff --git a/src/__stories__/useAsyncRetry.story.tsx b/src/__stories__/useAsyncRetry.story.tsx index 3cc8b3f87a..d17b1b5904 100644 --- a/src/__stories__/useAsyncRetry.story.tsx +++ b/src/__stories__/useAsyncRetry.story.tsx @@ -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((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(fnRetry); +const Demo = ({ delay }) => { + const state = useAsyncRetry( + () => + new Promise((resolve, reject) => { + setTimeout(() => { + if (Math.random() > 0.5) { + resolve('✌️'); + } else { + reject(new Error('A pseudo random error occurred')); + } + }, delay); + }), + [delay] + ); return (
- {loading ?
Loading...
: error ?
Error: {error.message}
:
Value: {value}
} - + {state.loading ? ( +

Loading...

+ ) : state.error ? ( +

Error: {state.error.message}

+ ) : ( +

Value: {state.value}

+ )} + +
{JSON.stringify(state, null, 2)}
); }; storiesOf('Side effects|useAsyncRetry', module) + .addDecorator(withKnobs) .add('Docs', () => ) - .add('Demo', () => ); + .add('Demo', () => { + const delay = number('delay', 1000, { range: true, min: 100, max: 5000, step: 100 }); + return ; + });