Prop Types



PropTypes

 

PropTypes ensure that the right type of props is passed to a component — and, conversely, that the receiving component is receiving the right type of props.

 

Installing PropTypes

 

To make use of PropTypes, you have to add the package as a dependency to your application by running

 

npm install prop-types

 

in the command line.

 

Using PropTypes

 

To use PropTypes, we first have to import PropTypes from the prop-types package. For example:

 

import { PropTypes } from 'prop-types';

 

We can then define the propTypes property on a component. For example:

 

MyComponent.propTypes = {};

 

Here propTypes is an object which contains key-value pairs, where the key is the name of the prop and the value is the type of the prop that can be passed to the component.

 

Example:

 

import React from 'react';

import { PropTypes } from 'prop-types';

class Student extends React.Component {

 

  render() {

    return (

      <div>

        <p>Student Name: {this.props.name}</p>

        <p>Age: {this.props.age}</p>

      </div>

    );

  }

}

 

Student.propTypes = {

  name: PropTypes.string,

  age: PropTypes.number

};

 

export default Student;

 

Here PropTypes.string and PropTypes.number are prop validators which can be used to make sure props received are of the right type. PropTypes has lots of validators, some of the most common use prop validators are:

 

MyComponent.propTypes = {

  propArray: PropTypes.array,

  propBool: PropTypes.bool,

  propFunc: PropTypes.func,

  propNumber: PropTypes.number,

  propObject: PropTypes.object,

  propString: PropTypes.string,

}

 

Passing Wrong Props

 

When a wrong or invalid prop is passed to the component, a warning or an error is shown in the JavaScript Console. For example:

 

import React from 'react';

import Student from './Student';

 

class App extends React.Component {

  render() {

    return (

      <div>

        <Student name="Mark" age="24" />

      </div>

    );

  }

}

 

export default App;

 

Here the age prop is passed as a string to the Student component. So React will show a warning in the JavaScript console.

 

We can also enforce passing props by using isRequired:

 

Pen.propTypes = {

  title: PropTypes.string.isRequired,

  url: PropTypes.string.isRequired,

  author: PropTypes.string.isRequired

};

 

Requiring Single Child

 

We can use PropTypes.element to enforce that only a single child can be passed to a component as children. For example:

 

import React from 'react';

import { PropTypes } from 'prop-types';

 

class Container extends React.Component {

render() {

    return (

      <div>

        {this.props.children}

      </div>

    );

  }

}

 

Container.propTypes = {

  children: PropTypes.element

};

 

export default Container;

 

If we try to pass more than 1 child to the component, we will get an error. For example:

 

import React from 'react';

import Container from './Container';

import Student from './Student';

 

class App extends React.Component {

render() {

    return (

      <div>

        <Container>

          <Student name="Mark" age="24" />

          <Student name="Peter" age="25" />

        </Container>

      </div>

    );

  }

}

 

export default App;

 

Advance PropTypes Validators

 

Apart from specifying whether a prop should be an array or an object, we can use these prop validators to specify which type of array or object can be passed as a prop. For example:

 

PropTypes.arrayOf()

 

We can use PropTypes.arrayOf() to specify which type of array can be passed as a prop. For example:

 

import React from 'react';

import { PropTypes } from 'prop-types';

 

class Numbers extends React.Component {

 

  render() {

    const numbers = this.props.numbers.map(number => <p>{number}</p>);

    return (

      <div>

        {numbers}

      </div>

    );

  }

}

 

Numbers.propTypes = {

  numbers: PropTypes.arrayOf(PropTypes.number)

};

 

export default Numbers;

Here only an array of number type can be passed as a prop. If we try to pass wrong type of array as a prop, we’d get a warning. For example:

 

import React from 'react';

import Numbers from './Numbers';

 

class App extends React.Component {

render() {

    return (

      <div>

        <Numbers numbers={['1', '2', '3']} />

      </div>

    );

  }

}

 

export default App;

 

PropTypes.objectOf()

 

Similar to PropTypes.arrayOf() we can use PropTypes.objectOf() to specify which type of object can be passed as a prop to the component. For example:

 

NamesList.propTypes = {

  names: PropTypes.objectOf(PropTypes.string)

}

 

Here, the only object which contains string type values can be passed as a prop to the component.

Default Props:

 

CatComponent.defaultProps = {
    catName: "Sandy",
    eyeColor: "deepblue",
    age: "120"
}

Show quoted text

Comments

Popular posts from this blog

Set Up Babel and Webpack

Typescript Setup

Typescript + React Setup