Using propTypes with Javascript 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
import PropTypes from 'prop-types';
class Greeting extends React.Component {
render() {
return (
Hello, {this.props.name}
);
}
}
Greeting.propTypes = {
name: PropTypes.string
};