Skip to content

Repository files navigation

prop-types-hook

Validate the props of React hooks using prop-types. Validation runs only in development — zero overhead in production.

Install

yarn add prop-types prop-types-hook

Usage

Pass the hook and its prop types to withPropTypes. Defaults live in the function signature.

import React from 'react'
import PropTypes from 'prop-types'
import { withPropTypes } from 'prop-types-hook'

const useMyHook = withPropTypes(
  ({ oneProp = { a: 1 }, twoProp = { b: 2 } } = {}) => {
    return React.useMemo(() => ({ ...oneProp, ...twoProp }), [oneProp, twoProp])
  },
  {
    oneProp: PropTypes.object,
    twoProp: PropTypes.object,
  }
)

Migrating from v1

withPropsValidation was renamed to withPropTypes in v2. Update your import:

-import { withPropsValidation } from 'prop-types-hook'
+import { withPropTypes } from 'prop-types-hook'

The second argument for prop types is now the preferred way to pass them, instead of attaching .propTypes to the hook function. The old .propTypes property approach still works as a fallback.

Author

Prolincur Technologies

About

Utility to validate the props of a React hook using prop-types

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages