Created
July 28, 2016 18:52
-
-
Save monder/f3f969ca8210191b550af130627b26cf to your computer and use it in GitHub Desktop.
react-resolve error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Resolver } from 'react-resolver'; | |
export default function resolve(func, prop = 'error') { | |
return function resolveErrorDecorator(Component) { | |
return class ResolveError extends Resolver { | |
static displayName = 'ResolveError' | |
static propTypes = { | |
children: React.PropTypes.object | |
} | |
componentWillReceiveProps() { | |
this.setState({ | |
pending: {}, | |
resolved: {} | |
}); | |
} | |
handleCatch(error) { | |
this.handledError = func(error); | |
if (this['ReactResolver.IS_CLIENT']) { | |
this.setState({ | |
pending: {}, | |
resolved: { [prop]: this.handledError } | |
}); | |
} | |
return { id: this['ReactResolver.ID'], pending: {}, resolved: { [prop]: this.handledError } }; | |
} | |
onResolve(promise) { | |
promise = promise | |
.catch((error) => { | |
return this.handleCatch(error); | |
}); | |
return super.onResolve(promise); | |
} | |
render() { | |
return <Component {...this.props} {...this.state.resolved} />; | |
} | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment