DEV Community

Max
Max

Posted on

getSnapshotBeforeUpdate property not existent

I'm attempting to use the method getSnapshotBeforeUpdate and I'm importing react in my file but I'm getting 'property doesn't exist' errors when using the code from React's documentation for maintaining DOM properties between component refreshes.

listRef = React.createRef();

getSnapshotBeforeUpdate(prevProps, prevState) {
    const { current } = this.listRef;
    const isScrolledToBottom = current.scrollTop + current.offsetHeight >= current.scrollHeight;
    return { isScrolledToBottom };
}

componentDidUpdate(prevProps, prevState, snapshot) {
    const { isScrolledToBottom } = snapshot;
    if (isScrolledToBottom) {
        this.listRef.current.scrollTop = this.listRef.current.scrollHeight;
    }
}

My problem is the .scrollTop and .offsetHeight are non existent properties of an unknown type. I was using this documentations: https://reactjs.org/docs/react-component.html#getsnapshotbeforeupdate

Any assistance to get these properties to exist is greatly appreciated :)

Top comments (0)