How to prevent building app if component prop types are invalid?

后端 未结 3 1925
忘了有多久
忘了有多久 2021-02-19 06:08

Here is a example for PropTypes:

import PropTypes from \'prop-types\';

class Greeting extends React.Component {
  render() {
    return (
      

Hello,

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-19 06:41

    The idea behind react is component base. Prop-types is runtime type checking for React props and similar objects. You can use prop-types to document the intended types of properties passed to components.

    In Greeting component, if you need name as a props, you have to define it on the prop-types. If name is required, you define it is required:

    Greeting.propTypes = {
      name: PropTypes.string.isRequired
    };
    

    To use Greeting, you just call it and pass props to it. I don't think we need to check what we have pass into Greeting component.

提交回复
热议问题