From 9a967169095e76cbefc727443b105fc3d9ceb387 Mon Sep 17 00:00:00 2001 From: jxom Date: Mon, 23 Apr 2018 13:58:58 +1000 Subject: [PATCH] Add reset function to children render props --- README.md | 15 ++++++++++++++- src/index.js | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d6291ba..2781e0c 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ export default () => ( - + @@ -84,6 +84,19 @@ export default () => (
PropTypeDefault valueDescription
children ({ response?: any, error?: any, load: Function, state: 'idle' | 'loading' | 'timeout' | 'success' | 'error' })N/A (required)
children ({ response?: any, error?: any, load: (...args: any) => ?Promise<any>, reset: Function, state: 'idle' | 'loading' | 'timeout' | 'success' | 'error' })N/A (required)
delay number300 Number of milliseconds before component transitions to loading state upon invoking fn/load.
loadOnMount booleanfalse Whether or not to invoke the fn on mount.
fn (...args: any) => Promise<any>N/A (required) The promise to invoke.
+##### `children` Render Props + + + + + + + + + + +
PropTypeDescription
response anyResponse from the resolved promise (`fn`)
error anyError from the rejected promise (`fn`)
load (...args: any) => ?Promise<any>Trigger to load `fn`
resetState () => voidReset state back to `idle`.
state 'idle' | 'loading' | 'timeout' | 'success' | 'error'Current state.
+ ### ``, ``, ``, ``, `` These components determine what node to render based on the loading/response state. diff --git a/src/index.js b/src/index.js index b5b8a71..1e53870 100644 --- a/src/index.js +++ b/src/index.js @@ -30,6 +30,7 @@ const statechart = { }, success: { on: { + RESET: 'idle', FETCH: 'loading', SUCCESS: 'success', ERROR: 'error' @@ -37,6 +38,7 @@ const statechart = { }, error: { on: { + RESET: 'idle', FETCH: 'loading', SUCCESS: 'success', ERROR: 'error' @@ -46,7 +48,13 @@ const statechart = { }; type Props = { - children: ({ response?: any, error?: any, load?: () => ?Promise }) => any, + children: ({ + response?: any, + error?: any, + load?: (...args: any) => ?Promise, + resetState: () => void, + state: string + }) => any, delay?: number, isErrorSilent?: boolean, loadOnMount?: boolean, @@ -149,9 +157,10 @@ class Loads extends Component { const state = machineState.value; return children({ error, + load: this.handleLoad, + resetState: () => this.transition('RESET'), response, - state, - load: this.handleLoad + state }); }; }