如果元素的 proptype 默认是什么?
If proptype of element what is the default?
如果我尝试将 proptype 设置为 PropTypes.element
,则不需要,正确的默认值是多少?
static propTypes = {
expandable: PropTypes.bool,
popover: PropTypes.element,
}
static defaultProps = {
expandable: false,
popover: () => {},
}
谢谢
默认值可以是:
React.createElement('div')
我认为undefined
应该可行。
static defaultProps = {
expandable: false,
popover: undefined,
}
React 中正确的默认组件或不存在的组件是 null
。您可以像这样在 render()
中使用它:
render() {
return (
<div>{this.props.popover ? this.props.popover : null}</div>
);
}
或者简单地在staticProps中定义它:
static defaultProps = {
expandable: false,
popover: null,
}
如果我尝试将 proptype 设置为 PropTypes.element
,则不需要,正确的默认值是多少?
static propTypes = {
expandable: PropTypes.bool,
popover: PropTypes.element,
}
static defaultProps = {
expandable: false,
popover: () => {},
}
谢谢
默认值可以是:
React.createElement('div')
我认为undefined
应该可行。
static defaultProps = {
expandable: false,
popover: undefined,
}
React 中正确的默认组件或不存在的组件是 null
。您可以像这样在 render()
中使用它:
render() {
return (
<div>{this.props.popover ? this.props.popover : null}</div>
);
}
或者简单地在staticProps中定义它:
static defaultProps = {
expandable: false,
popover: null,
}