【发布时间】:2020-05-27 15:02:08
【问题描述】:
更新
也许解决方案在于我声明组件的方式:
import React, { FunctionComponent } from 'react';
export const ChapterDescription: FunctionComponent<
ChapterDescription
> = (
{
description,
name,
}: ChapterDescription,
) => (
<div>
<h3>{name}</h3>
<p>{description}</p>
</div>
);
当我只为我的组件使用reactjs 时,我可以轻松地像这样有条件地渲染组件(检查chapters 数组是否有项目并且仅 然后渲染组件) :
<OtherComponent/>
<div>
{chapters.length > 0 && <ChapterDescription
name={name}
description={description}
/>}
</div>
<OtherComponent2 />
当我在打字稿中尝试时得到错误:
Type 'false | Element' is not assignable to type 'Element'.
Type 'false' is not assignable to type 'Element'.ts(2322)
条件渲染不再是不好的做法吗? 我该如何处理这种情况?
【问题讨论】:
-
通常情况下,孩子们是假的完全没问题。该父组件对其道具有什么类型定义(我认为该组件是“LesserContentColumn”)?它可能对类型的限制超出了应有的范围
标签: javascript reactjs typescript ecmascript-6