简体   繁体   English

为什么它抱怨类型 {intrinsicattributes && true} 或类型 {intrinsicattributes && false} 不能使用 react 和 typescript 分配?

[英]Why does it complain about type {intrinsicattributes && true} or type {intrinsicattributes && false} are not assignable using react and typescript?

i have code like below,我有如下代码,

 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; 
  }

this works fine but refactored this code这工作正常,但重构了这段代码

{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

But this displays link for email us even though !isCount and i see error但这会显示 email 我们的链接,即使 !isCount 并且我看到错误

error "type {isCount: boolean is not assignable to type {intrinisicAttributes && false} or type {intrinsicAttributes && true}错误“类型 {isCount: boolean 不可分配给类型 {intrinisicAttributes && false} 或类型 {intrinsicAttributes && true}

Could someone help me fix this.有人可以帮我解决这个问题。 thanks.谢谢。

The first arg in React functional components is their props which is an object of received props. React 功能组件中的第一个 arg 是它们的 props,它是接收到的 props 的 object。 So change this line:所以改变这一行:

const RenderLink = ( isCount: boolean ) =>

To:至:

interface Props {
   isCount: boolean
}
const RenderLink: React.FC<Props> = ({isCount}) =>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与 TypeScript 反应:类型不可分配给类型“IntrinsicAttributes” - React with TypeScript: Type is not assignable to type 'IntrinsicAttributes' onClick 无法分配给 TypeScript 反应中的类型“IntrinsicAttributes”错误 - onClick not assignable to type 'IntrinsicAttributes' error in TypeScript React 类型“{}”不可分配给类型“IntrinsicAttributes &amp; {}” - Type '{ }' is not assignable to type 'IntrinsicAttributes & {}' 类型不可分配给类型 IntrinsicAttributes - Type is not assignable to type IntrinsicAttributes 类型“{}”不可分配给类型“IntrinsicAttributes” - Type '{}' is not assignable to type 'IntrinsicAttributes' TypeScript错误:类型“…”不能分配给类型“ IntrinsicAttributes&Number” - TypeScript error : Type '…' is not assignable to type 'IntrinsicAttributes & Number' 使用 React Typescript 的状态:类型“IntrinsicAttributes 和 IntrinsicClassAttributes”上不存在属性 - State using React Typescript: Property does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes 对于具有扩展的可区分联合的 React 组件,TypeScript 错误“不可分配给类型‘IntrinsicAttributes’” - TypeScript error "not assignable to type 'IntrinsicAttributes'" for React component with extended discriminated union 不可分配到类型“IntrinsicAttributes”(React.js 和 TypeScript.js) - is not assignable to type 'IntrinsicAttributes (React.js and TypeScript.js) 样式组件 + typescript:“as”不可分配给类型 IntrinsicAttributes - Styled components + typescript: "as" is not assignable to type IntrinsicAttributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM