GithubHelp home page GithubHelp logo

better-prop-types's Introduction

BetterPropTypes

A DSL for less verbse propTypes declarations in React.

Installation

npm install better-prop-types

Usage

When declaring your propTypes, instead of a plain object, pass an object to the function exported from the better-prop-types package. See below for more details.

import pt from 'better-prop-types';

export default class MyReactComponent extends React.Component {
  static propTypes = pt({
    // ... BetterPropTypes declaration here
  })
}

Differences from normal prop-types

BetterPropTypes makes two assertions:

  1. Most props should usually be required by default, and optional props should be made explict (rather than the other way around)
  2. Nested object types are a pain to declare due to lots of nested PropTypes.shape({...}).isRequired calls

To remedy this, BetterPropTypes exports an interface just like the normal prop-types package, but with the following changes:

  1. All types are required by default; call .opt on them to make them optional
  2. Instead of PropTypes.shape({...}).isRequired, for required objects, simply use plain objects
  3. Instead of PropTypes.shape({...}) for optional objects, use pt.opt({...})
  4. Your top-level propTypes declaration should be an object passed to the pt function (the main package export) instead of a plain object

Example

Before:

import PropTypes from 'prop-types';
// ...
static propTypes = {
  callback: PropTypes.func.isRequired,
  data: PropTypes.shape({
    id: PropTypes.string.isRequired,
    user: PropTypes.shape({
      name: PropTypes.string.isRequired,
      slug: PropTypes.string
    }).isRequired,
    friends: PropTypes.arrayOf(
      PropTypes.shape({
        id: PropTypes.string.isRequired,
        user: PropTypes.shape({
          name: PropTypes.string.isRequired
        }).isRequired
      }).isRequired
    )
  }).isRequired
}

After:

import pt from 'better-prop-types';
// ...
static propTypes = pt({
  callback: pt.func,
  data: {
    id: pt.string,
    user: {
      name: pt.string,
      slug: pt.string.opt,
    },
    friends: pt.arrayOf({
      id: pt.string,
      user: {
        name: pt.string,
      },
    }).opt
  },
})

better-prop-types's People

Contributors

binarymuse avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.