Checking prop types in React

Amous

I was reading the React.Component section in the official React documentation. Everything made sense except for the part regarding propTypes. The docs state the following:

In production mode, propTypes checks are skipped for efficiency.

Say I have the following code:

class Sample extends React.Component {
    render() {
        return (
            <div>Hello {this.props.name}</div>
        );
    }
}

Sample.propTypes = {
    name: React.PropTypes.string
};

Does the docs imply that in production my type checks against props will be skipped? If yes, how should I be checking prop types?

Timo

You don't check against the prop types yourself at all, React does that for you.

However, as the docs say, only as long as you are in development mode. Every prop type check is essentially a function call that uses processing power and memory.


While you are in development, knowing that one of your props has the wrong type makes this cost a worthy trade-off.

Once you are in production, your app should be tested thoroughly enough already that none of your prop type validations fail anymore anyway.

For this reason, they are skipped to make your app a bit more efficient instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Prop-types checking in react component

Extending react prop-types validation with custom checking

React interdependent prop types

Need to import prop types in React?

prop types not working in react application

React prop-types error

React Prop Types vs Typescript

React and Typescript - incorrect Types Checking

Is it possible to check prop types as case insensitive on React prop-types?

React Prop Types - On false prop type, reset to default?

React - prop-types is marked as required

Syntax for Typescript prop types for React component?

TypeScript, if clause of prop in React with differene types and if statements

Typescript/React - Multiple types for one prop not working

Extract react component prop types with generics

React Component Exact TypeScript Prop Types

missing in props validation react/prop-types

react/prop-types eslint error in typescript react component

react prop-types: reusable validator from existing prop-types

(ESLint) Definition for rule 'react/jsx-sort-prop-types' was not found

Object Destructuring eslint throws react/prop-types

TypeScript types for React component where prop is an array of objects?

React prop types with TypeScript - how to have a function type?

Define prop types using flow for stateless functional components in react native

React Prop Types Different Required Props Depending on Flag

Why is React saying “Invalid prop `children`” types is object not function?

Importing react libraries into Codepen (namely prop-types)

Dispatch action is missing in props validation [react/prop-types]

React, typescript and prop-types: how to use own type?