【发布时间】:2017-06-23 18:40:51
【问题描述】:
我有一个 React 组件,它需要孩子输入。 PropTypes 设置如下:
static propTypes = {
children: PropTypes.arrayOf(PropTypes.element).isRequired
};
但是当内联条件变为假时,我会收到警告,即:
{isLoggedIn && <div>You are logged in</div>}
警告:失败的道具类型:提供给
boolean的无效道具children[0]类型为WelcomeComponent,需要一个ReactElement。
文档并没有真正的帮助。 https://facebook.github.io/react/docs/conditional-rendering.html
【问题讨论】:
-
您是否尝试过明确返回
null?{isLoggedIn && <div>You are logged in</div> || null} -
@Tholle null 有效。我想知道文档是否错误或 propTypes 验证规则。
-
@Throlle 我试过了,但它给了我一个错误
Cannot read property 'type' of null
标签: reactjs