[英]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.