npm install --save prop-types
import PropTypes from "prop-types";
MyComponent.propTypes = {
name: PropTypes.string,
id: PropTypes.number.isRequired,
img: PropTypes.string
grades: PropTypes.arrayOf(PropTypes.number),
friends: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
})
),
list: PropTypes.arrayOf(PropTypes.any),
};
MyComponent.defaultProps = {
img: "https://picsum.phones/200/300",
}
import PropTypes from 'prop-types';
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.name}</h1>
);
}
}
Greeting.propTypes = {
name: PropTypes.string
};
npm install --save prop-types
import React from 'react';
import PropTypes from 'prop-types';
const Example = (props) => {
return (
<div>
<p>Some prop: {props.some}</p>
<p>Other prop: {props.other}</p>
</div>
);
};
Example.propTypes = {
some: PropTypes.string.isRequired,
other: PropTypes.number.isRequired
};
export default 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
};
import PropTypes from 'prop-types'; // ES6var PropTypes = require('prop-types'); // ES5 with npm
type OscarProp = {
children: React.ReactNode;
};
type OscarProp = {
children: React.ReactNode;
};
itemName.propTypes = {
props: PropTypes.dataType.isRequired
};
//You can now use `PropTypes.elementType`
// to validate for Component type props
import PropTypes from 'prop-types'; // ES6
var PropTypes = require('prop-types'); // ES5 with npm