Redux Resource Prop Types
A collection of prop-types objects.
Other Guides
Old Documentation
Migration Guides
Installation
Install redux-resource-prop-types with npm:
npm install redux-resource-prop-types --save
Then, import the prop types that you need:
import { resourcesPropType } from 'redux-resource-prop-types';Usage
If you're using React, refer to the Typechecking with PropTypes guide on how to use the exported prop types. If you're not using React, refer to the documentation of the prop-types library.
We recommend using the prop types in this library to build your own prop types, which you can reuse throughout your application.
idPropType
idPropTypeValidates a single resource ID.
Tip: This prop type requires that your IDs be either strings or numbers.
import PropTypes from 'prop-types';
import { idPropType } from 'redux-resource-prop-types';
MyComponent.propTypes = {
selectedBookIds: PropTypes.arrayOf(idPropType).isRequired
};
mapStateToProps(state) {
return {
selectedBookIds: state.books.selectedIds
};
}statusPropType
statusPropTypeValidates the object returned by getStatus.
import { getStatus } from 'redux-resource';
import { statusPropType } from 'redux-resource-prop-types';
MyComponent.propTypes = {
booksReadStatus: statusPropType
};
mapStateToProps(state) {
return {
bookReadStatus: getStatus(state, 'books.meta[23].readStatus');
};
}requestStatusPropType
requestStatusPropTypeValidates that a value is one of the requestStatuses. Typically, you'll want to use statusPropType instead, but this can be useful when verifying the structure of your slice.
import { requestStatusPropType } from 'redux-resource-prop-types';
MyComponent.propTypes = {
bookRequestStatus: requestStatusPropType
};
mapStateToProps(state) {
return {
bookRequestStatus: state.books.meta[23].readStatus
};
}resourcePropType
resourcePropTypeValidates a resource. Similar to PropTypes.shape(), except that it enforces an ID.
import PropTypes from 'prop-types';
import { resourcePropType } from 'redux-resource-prop-types';
MyComponent.propTypes = {
book: resourcePropType({
name: PropTypes.string.isRequired,
releaseYear: PropTypes.number.isRequired
})
};
mapStateToProps(state) {
return {
book: state.books.resources[23]
};
}requestPropType
requestPropTypeValidates a request object. Similar to PropTypes.shape(), except that it enforces ids, status, requestKey and resourceType. Typically, you won't need to use this, but it can be useful to verify the structure of your state.
import PropTypes from 'prop-types';
import { requestPropType } from 'redux-resource-prop-types';
MyComponent.propTypes = {
searchRequest: requestPropType({
statusCode: PropTypes.number.isRequired
})
};
mapStateToProps(state) {
return {
searchRequest: state.books.requests.search
};
}Last updated