我有如下代码,
function Parent() {
const count1 = 2;
const count2 = 4;
const isCount = count1 < 0 || count2 < 0; //setting isCount here
return show ? (
<Dialog>
<Body>soemthing</Body>
<Actions
isAdminAndisCount={isAdminAndisCount}
>
{((isAdmin && !isCount) || !isAdmin) && (
<Text onClick={onHide}>Close</Text>
)}
{isAdmin ? ( //to refactor this
isCount ? (
<a href="eee">email us</a>
) : (
<a href="mmm">add</a>
)
) : null}
</Actions>
</Dialog>
) : null;
}
这工作正常,但重构了此代码
{isAdmin ? ( //to refactor this
isCount ? (
<a href="eee">email us</a>
) : (
<a href="mmm">add</a>
)
) : null}
**TO**
const RenderLink = ( isCount: boolean ) =>
isCount ? <a href="eee">email us</a> : <a href="mmm">add</a>;
{isAdmin && <RenderLink isCount={isCount} />} //here is the error
但这会显示给我们发送电子邮件的链接,即使 !isCount 并且我看到错误
错误"类型 {isCount: 布尔值不可分配给类型 {intrinisicAttributes && false} 或类型 {intrinsicAttributes && true}
有人可以帮我解决这个问题吗。谢谢。
React 功能组件中的第一个参数是它们的 props,它是接收 props 的对象。所以改变这一行:
const RenderLink = ( isCount: boolean ) =>
自:
interface Props {
isCount: boolean
}
const RenderLink: React.FC<Props> = ({isCount}) =>