Skip to content

Commit 8902849

Browse files
committed
feat($onError): add onError prop
1 parent a68488f commit 8902849

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ MyUniversalComponent.doSomething()
247247
- `error: new Error`
248248
- `onBefore`: `({ isMount, isSync, isServer }) => doSomething(isMount, isSync, isServer)`
249249
- `onAfter`: `({ isMount, isSync, isServer }, Component) => doSomething(Component, isMount, etc)`
250+
- `onError`: `error => handleError(error)`
250251
251252
### `isLoading` + `error`:
252253
You can pass `isLoading` and `error` props to the resulting component returned from the `universal` HoC. This has the convenient benefit of allowing you to continue to show the ***same*** `loading` component (or trigger the ***same*** `error` component) that is shown while your async component loads *AND* while any data-fetching may be occuring in a parent HoC. That means less jank from unnecessary re-renders, and less work (DRY).
@@ -308,6 +309,8 @@ const MyComponent = ({ dispatch, isLoading }) =>
308309
309310
> Keep in mind if you call `setState` within these callbacks and they are called during `componentWillMount`, the `state` change will have no effect for that render. This is because the component is already in the middle of being rendered within the parent on which `this.setState` will be called. You can use *Redux* to call `dispatch` and that will affect child components. However, it's best to use this primarily for setting up and tearing down loading state on the client, and nothing more. If you chose to use them on the server, make sure the client renders the same thing on first load or you will have checksum mismatches.
310311
312+
- `onError` is similar to the `onError` static option, except it operates at the component level. Therefore you can bind to `this` of the parent component and call `this.setState()` or `this.props.dispatch()`. Again, it's use case is for when you want to show error information elsewhere in the UI besides just the place that the universal component would otherwise render :)
313+
311314
312315
313316
## Universal Demo

src/flowTypes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ export type State = { error?: any, Component?: ?any }
9393
type Info = { isMount: boolean, isSync: boolean, isServer: boolean }
9494
type OnBefore = Info => void
9595
type OnAfter = (Info, any) => void
96+
type OnErrorProp = (error: { message: string }) => void
9697

9798
export type Props = {
9899
error?: ?any,
99100
isLoading?: ?boolean,
100101
onBefore?: OnBefore,
101-
onAfter?: OnAfter
102+
onAfter?: OnAfter,
103+
onError?: OnErrorProp
102104
}
103105

104106
export type GenericComponent<Props> =

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ export default function universal<Props: Props>(
182182
onAfter(info, Component)
183183
}
184184
}
185+
else if (error && this.props.onError) {
186+
const { onError } = this.props
187+
const info = { isMount, isSync, isServer }
188+
onError(error)
189+
}
185190

186191
this.setState(state)
187192
}

0 commit comments

Comments
 (0)