Code Monkey home page Code Monkey logo

proptypes's Introduction

proptypes

React's PropTypes, packaged up as a standalone module.

DISCLAIMER: This is literally copied from the React 0.14 codebase. If you're using PropTypes with React, it would be silly to reference this standalone version.

Usage

function check(props, propTypes) {
  for (let prop in propTypes) {
    if (propTypes.hasOwnProperty(prop)) {
      let err = propTypes[prop](props, prop, 'name', 'prop');
      if (err) {
        console.warn(err);
        return false;
      }
    }
  }
  return true;
}

let valid = check({
  a: 42,
  b: 'News'
}, {
  a: PropTypes.number,
  b: PropTypes.oneOf(['News', 'Photos'])
});

valid;  // true

Production build

Disabling

With webpack:

module.exports = {
  resolve: {
    alias: {
      proptypes: 'proptypes/disabled',
    }
  }
};

Removing the definitions with React

When you are using this package with React like API, you might want to save bandwidth by removing the definitions. You can use babel-plugin-transform-react-remove-prop-types for that use case, for instance:

// In
const Baz = (props) => (
  <div {...props} />
);

Baz.propTypes = {
  className: PropTypes.string
};

// Out
const Baz = (props) => (
  <div {...props} />
);

License

BSD

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.