ReactJs: What should the PropTypes be for this.props.children?

前端 未结 8 1138
梦如初夏
梦如初夏 2020-12-04 05:49

Given a simple component that renders its children:

class ContainerComponent extends Component {
  static propTypes = {
    children: PropTypes.object.isRequ         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 06:54

    Example:

    import React from 'react';
    import PropTypes from 'prop-types';
    
    class MenuItem extends React.Component {
        render() {
            return (
                
  • {this.props.children}
  • ); } } MenuItem.defaultProps = { href: "/", children: "Main page" }; MenuItem.propTypes = { href: PropTypes.string.isRequired, children: PropTypes.string.isRequired }; export default MenuItem;

    Picture: Shows you error in console if the expected type is different

提交回复
热议问题