繁体   English   中英

如何使用 react 和 typescript 修复错误 object 可能未定义?

[英]How to fix the error object could be possibly undefined using react and typescript?

在条件内使用变量时,我收到错误 object 可能未定义。

下面是片段,

render = () => {
    const value_to_check = 8 //got by some http request could be any number
    return (
        <>
        {condition1 && condition2 && (
            value_to_check < 1 ? null : ( //here is where i get the pycharm 
            //error object could be undefined
            <div1>something</div1>
        ))}
    )
}

我该如何解决该错误。 谢谢。

 render = () => { const value_to_check = 8 //got by some http request could be anything return ( <> {condition1 && condition2 && ( // value_to_check might be undefined and u still compare it with < 1. value_to_check < 1? null: ( //here is where i get the pycharm //error object could be undefined <div1>something</div1> ))} // Solution ( Notes: I don't recommend the style below, it's hard to read) {condition1 && condition2 && value_to_check && ( value_to_check < 1? null: <div>Something</div>)} ) }
 <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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM