Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example on how to go about using a prop type validator inside a custom validator function.
- // In this case, we want to check of the given prop is either a string, a number, a bool,
- // or an instance of Moment. However, it's recommended by the package authors to use
- // the `.isMoment` function instead of directly checking with `instanceof`
- import React from 'react'
- import { oneOfType, string, number, bool } from 'prop-types'
- import moment from 'moment'
- ExampleComponent.propTypes = {
- initialValue: function(props, propName, componentName, ...rest) {
- const propTypeError = oneOfType([string, number, bool])(
- props,
- propName,
- componentName,
- ...rest
- )
- if (propTypeError) {
- const propIsInstanceOfMoment =
- props[propName] && moment.isMoment(props[propName])
- if (propIsInstanceOfMoment) {
- return null
- }
- propTypeError.message += ` Should be of type '${String.name}', '${
- Number.name
- }', '${Boolean.name}' or 'Moment'.`
- throw propTypeError
- }
- },
- }
- export default function ExampleComponent({ initialValue }) {
- return <h1>Example component</h1>
- }
Add Comment
Please, Sign In to add comment