1

I am checking to see if the user is on a device smaller than 768px, and if they are I want to set isTop to false. I am running into a few issues. Everything looks to be okay, but I keep getting this error:

TypeError: _this2.state.isTop is not a function

What I am working with:

componentDidMount() {
    this.updateWindowDimensions();
    window.addEventListener('resize', this.updateWindowDimensions);
    document.addEventListener('scroll', () => {
        const isTop = window.scrollY < window.innerHeight - 50;
        if (isTop !== this.state.isTop) {
          this.setState({ isTop })
        }
    });

    this.checkWidth = () => {
        const match = window.matchMedia(`(max-width: 768px)`);
        if (match) {
            this.state.isTop(false);
        }
    };

    this.checkWidth();
    window.addEventListener('resize', this.checkWidth);
}

Any help would be appreciated

Palle Due
  • 5,929
  • 4
  • 17
  • 32
brooksrelyt
  • 3,925
  • 5
  • 31
  • 54

2 Answers2

5

Your syntax seems to be incorrect for setting the isTop state to false. (Source: docs)

Try this instead

this.setState({isTop: false});
nash11
  • 8,220
  • 3
  • 19
  • 55
-1

You need to declare state in constructor

constructor(props) {
    super(props);
    this.state = {isTop: false}
}

To set state

this.setState({
    isTop: "YourValue"
});

To Fetch

this.state.isTop;

Not like you did

this.state.isTop(false);
brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
paragxviii
  • 220
  • 1
  • 9