Skip to content

Instantly share code, notes, and snippets.

@hanipcode
Created February 27, 2017 03:06
Show Gist options
  • Select an option

  • Save hanipcode/104c84cb79928004fb1d8a6f28a86875 to your computer and use it in GitHub Desktop.

Select an option

Save hanipcode/104c84cb79928004fb1d8a6f28a86875 to your computer and use it in GitHub Desktop.
error handling multiple error
// sisa dari aplikasi kita
//fungsi render
render() {
//alternative 1, mapping di render
const { formReducer, loginReducer, postReducer, errorList } = this.props;
//bisa di masukin array dulu sih kalau mau, tapi saya prefer ini
//karena masih reasonable untuk di stack di if
if (formReducer.get('error') ||
loginReducer.get('error') ||
postReducer.get('error')) {
//seperti awal, alternative menampilkan error page
return <ErrorPage ..props />
}
//alternative 2, mapping di mapToState (lihat di bawah)
return (
<View>
// cek dulu kalau size nya 0 abaikan block ini
{ errorList.length > 0 && errorList.map( errorObj =>
<ErrorPopup type={errorObj.type} message={errorObj.message} />
)
}
<MainComponent />
</View>
);
}
// gunakan map state to props untuk mapping error
const mapStateToProps = state => ({
errorList: [state.formReducer.get('error'), state.postReducer.get('error'), state.loginReducer.get('error')]
// .. other mapping
});
connect(mapStateToProps)(YourComponent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment