diff --git a/src/RecyclerFlatList.tsx b/src/RecyclerFlatList.tsx index 889827626..0d1f1b917 100644 --- a/src/RecyclerFlatList.tsx +++ b/src/RecyclerFlatList.tsx @@ -13,12 +13,13 @@ import { RecyclerListView, RecyclerListViewProps, } from "recyclerlistview"; -import invariant from "invariant"; import AutoLayoutView, { BlankAreaEventHandler } from "./AutoLayoutView"; import ItemContainer from "./CellContainer"; import WrapperComponent from "./WrapperComponent"; import GridLayoutProviderWithProps from "./GridLayoutProviderWithProps"; +import CustomError from "./errors/CustomError"; +import ExceptionList from "./errors/ExceptionList"; export interface RecyclerFlatListProps extends FlatListProps { // TODO: This is to make eslint silent. Out prettier and lint rules are conflicting. @@ -108,25 +109,25 @@ class RecyclerFlatList extends React.PureComponent< constructor(props) { super(props); - this.setup(); - } - - private setup() { - const refreshingPrecondition = !( - this.props.onRefresh && typeof this.props.refreshing !== "boolean" - ); - const message = - 'Invariant Violation: `refreshing` prop must be set as a boolean in order to use `onRefresh`, but got `"undefined"`'; - invariant(refreshingPrecondition, message); - if (this.props.estimatedListSize) { - if (this.props.horizontal) { - this.listFixedDimensionSize = this.props.estimatedListSize.height; + this.validateProps(); + if (props.estimatedListSize) { + if (props.horizontal) { + this.listFixedDimensionSize = props.estimatedListSize.height; } else { - this.listFixedDimensionSize = this.props.estimatedListSize.width; + this.listFixedDimensionSize = props.estimatedListSize.width; } } } + private validateProps() { + if (this.props.onRefresh && typeof this.props.refreshing !== "boolean") { + throw new CustomError(ExceptionList.refreshBooleanMissing); + } + if (!(this.props.estimatedItemSize > 0)) { + throw new CustomError(ExceptionList.estimatedItemSizeMissing); + } + } + // Some of the state variables need to update when props change static getDerivedStateFromProps( nextProps: RecyclerFlatListProps, diff --git a/src/errors/CustomError.ts b/src/errors/CustomError.ts new file mode 100644 index 000000000..1d7d2fef4 --- /dev/null +++ b/src/errors/CustomError.ts @@ -0,0 +1,10 @@ +export default class CustomError extends Error { + constructor(exception: Exception) { + super(`${exception.type}: ${exception.message}`); + this.name = exception.type; + } +} +export interface Exception { + type: string; + message: string; +} diff --git a/src/errors/ExceptionList.ts b/src/errors/ExceptionList.ts new file mode 100644 index 000000000..1afd26258 --- /dev/null +++ b/src/errors/ExceptionList.ts @@ -0,0 +1,15 @@ +import { Exception } from "./CustomError"; + +const ExceptionList: { [key: string]: Exception } = { + estimatedItemSizeMissing: { + message: + "`estimatedItemSize` is a required prop in RecyclerFlatList. Please provide a value that is greater than 0.", + type: "InvalidPropException", + }, + refreshBooleanMissing: { + message: + "`refreshing` prop must be set as a boolean in order to use `onRefresh`, but got `undefined`", + type: "InvariantViolation", + }, +}; +export default ExceptionList;