why use proptypes in react code example

Example 1: use propTypes in react function

import React from 'react';
import { PropTypes } from 'prop-types';

const student = (props) => {
  return (
    

Student Name: {props.name}

Age: {props.age}

); }; student.propTypes = { name: PropTypes.string, age: PropTypes.number }; export default student;

Example 2: react proptypes example

// proptypes using class component
Detaljer.PropTypes = {
  detaljer: PropTypes.string.isRequired,
  feilkode: PropTypes.string,
  removeEvent: PropTypes.string.isRequired
};

// proptypes using function component
Detaljer.propTypes = {
  detaljer: PropTypes.string.isRequired,
  feilkode: PropTypes.string,
  removeEvent: PropTypes.string.isRequired
};

Example 3: proptypes.objectof

Pokemon.propTypes = {
  pokemon: PropTypes.shape({
    name: PropTypes.string,
    id: PropTypes.number,
    base_stamina: PropTypes.number,
    base_defense: PropTypes.number
  })
}

Tags:

Misc Example