简体   繁体   English

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

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

while using a variable within condition i get the error object could be possibly undefined.在条件内使用变量时,我收到错误 object 可能未定义。

Below is the snippet,下面是片段,

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

How could i fix that error.我该如何解决该错误。 thanks.谢谢。

 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.

相关问题 反应 typescript state Object 可能是“未定义”错误 - react typescript state Object is possibly 'undefined' error React Tooltip 组件与 TypeScript 在我的 useRef 元素上出现“可能未定义”错误,不知道如何修复 - React Tooltip component with TypeScript getting a 'Possibly Undefined' error on my useRef element, not sure how to fix 如何修复“对象可能未定义”? - How to fix “Object is possibly undefined”? 如何使用 react 和 typescript 修复 object 可能是可重用组件中未定义的错误? - How to fix the object is probably undefined error within a reusable component using react and typescript? 如何使用 react 和 typescript 修复错误无法读取未定义的属性? - How to fix error cannot read property of undefined using react and typescript? Object 可能是可选链接上的“未定义”错误 Typescript - Object is possibly 'undefined' Error on Optional Chaining Typescript 打字稿:对象可能是“未定义” - Typescript: Object is possibly 'undefined' Object 可能在 TypeScript 中未定义 - Object is possibly undefined in TypeScript 对象可能是“未定义”打字稿 - Object is possibly 'undefined' typescript 打字稿抱怨反应组件可选道具可能未定义 - Typescript complains that react component optional prop could possibly be undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM