Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to avoid Typescript Object is possibly 'null'. TS2531

$
0
0

I am trying to get the scroll position using the following code:

    useEffect (()=>{    document.addEventListener("scroll", e => {        let scrolled = document.scrollingElement.scrollTop;        if (scrolled >= 5){           setPos("moved")        } else {           setPos("top")        }    })  },[])

Typescript complains about the document.scrollingElement.scrollTop saying that it is possibly null. How do I avoid this error and keep typescript happy?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>