简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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