简体   繁体   English

如何使用 react 和 typescript 修复 object 可能是可重用组件中未定义的错误?

[英]How to fix the object is probably undefined error within a reusable component using react and typescript?

I am getting error object is possibly undefined when i use the variable "items_count".当我使用变量“items_count”时,我收到错误 object 可能未定义。

what i am trying to do?我想做什么? I have an ContainerComponent that uses ContentComponent which displays an icon.我有一个使用 ContentComponent 的 ContainerComponent ,它显示一个图标。 This ContentComponent is a reusable component.这个 ContentComponent 是一个可重用的组件。

Below is my ContainerComponent下面是我的 ContainerComponent

render = () => {
    const items_count = 10; //got by http request and its value could be undefined, 0 or anything 
    //above 0
    return (
        <Wrapper>
            <LeftSide>
                <ContainerComponent>
                    <ContentComponent>
                        items_count=10
                        icon_name='add'
                    </ContentComponent>
               </ContainerComponent>
           </LeftSide>
           <RightSide>
               <ContainerComponent>
                   <ContentComponent>
                       icon_name='delete'
                   </ContentComponent>
               </ContainerComponent>
           </RightSide>
       </Wrapper>
   ) 

} }

ContentComponent renders the Icon and it looks like below, ContentComponent 呈现图标,如下所示,

const ContentComponent: React.FC<Props> = ({
    icon_name,
    items_count,
}) => {
    return (
        {condition1 && condition2 &&
            (icon_name === 'add' && items_count < 1 ? null : ( //here i get error object items_count 
            //could be possibly undefined
                <firstdiv>
                   <seconddiv>
                       <Icon name={icon_name} />
                   </seconddiv>
                </firstdiv>
            ))}
    )
}

To fix the error i have tried something like below为了修复错误,我尝试了类似下面的方法

return (
    {condition1 && condition2 && items_count && //added a check here
        (icon_name === 'add' && items_count < 1 ? null : (
            <firstdiv>
                <seconddiv>
                    <Icon name={icon_name} />
                </seconddiv>
            </firstdiv>
     ))}
)

But as you see in the ContainerComponent RightSide component i dont pass the items_count and within ContentsComponent i want this icon with name delete to render always irrespective of items_count value.但是正如您在 ContainerComponent RightSide 组件中看到的那样,我没有传递 items_count 并且在 ContentsComponent 中我希望这个名为 delete 的图标始终呈现,而与 items_count 值无关。 and icon with name add to render only when items_count value is greater than 0.只有当 items_count 值大于 0 时才会添加带有名称的图标。

that check in my snippet works but gives error item_count could be possibly undefined.签入我的代码片段有效,但给出的错误 item_count 可能未定义。 how can i fix it..could someone help me with this.我该如何解决它..有人可以帮我解决这个问题。 thanks.谢谢。

This will always show the delete icon, and sometimes the add icon will show side by side with the delete icon这将始终显示delete图标,有时add图标会与删除图标并排显示

 /* U just need to change the structure of your code */ const ContentComponent: React.FC<Props> = ({ icon_name, items_count, }) => { const renderIcon = () => { if(?condition1 ||:condition2 || !items_count) return null return ( <firstdiv> <seconddiv> <Icon name='delete'} /> {items_count > 0 ? <Icon name='add'} /> : null } </seconddiv> </firstdiv> ) } return ( <> {renderIcon()} </> ) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

暂无
暂无

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

相关问题 如何使用 react 和 typescript 修复错误 object 可能未定义? - How to fix the error object could be possibly undefined using react and typescript? 如何使用 react 和 typescript 修复错误无法读取未定义的属性? - How to fix error cannot read property of undefined using react and typescript? 如何使用 React &amp; TypeScript 创建可重用的输入组件? - How to create a reusable Input component with React & TypeScript? React Tooltip 组件与 TypeScript 在我的 useRef 元素上出现“可能未定义”错误,不知道如何修复 - React Tooltip component with TypeScript getting a 'Possibly Undefined' error on my useRef element, not sure how to fix 如何修复native native中的&#39;undefined is not object&#39;错误 - How to fix 'undefined is not an object' error in react native 使用 React 和 Typescript 创建可重用的按钮组件,但出现不可分配的类型错误 - Creating reusable Button Component with React and Typescript with not assignable type error 如何使用 React 循环可重用组件 - how to loop a reusable component using react 如何使 typescript 中的自定义和可重用表组件做出反应? - how to make a custom and reusable table component in typescript react? 使用 React & TypeScript 创建可重用的表单输入组件 - Create a reusable Form Input component with React & TypeScript 反应 typescript state Object 可能是“未定义”错误 - react typescript state Object is possibly 'undefined' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM