If proptype of element what is the default?

2019-07-16 03:35发布

if I try to set a proptype as PropTypes.element, not required, what is the proper default?

static propTypes = {
    expandable: PropTypes.bool,
    popover: PropTypes.element,
  }

  static defaultProps = {
    expandable: false,
    popover: () => {},
  }

Thanks

3条回答
够拽才男人
2楼-- · 2019-07-16 04:03

The proper default, or non existing component, in React is null. You can use it in render() like that:

render() {
    return (
        <div>{this.props.popover ? this.props.popover : null}</div>
    );
}

or simply define it in staticProps:

static defaultProps = {
    expandable: false,
    popover: null,
}
查看更多
成全新的幸福
3楼-- · 2019-07-16 04:05

The default value could be:

React.createElement('div')
查看更多
趁早两清
4楼-- · 2019-07-16 04:05

I think undefinedshould work.

static defaultProps = {
  expandable: false,
  popover: undefined,
}
查看更多
登录 后发表回答