如何在if条件内设置状态

时间:2019-07-09 15:30:01

标签: reactjs

我有一个if条件,在满足条件下,我想将状态设置为true,否则将状态设置为false。

componentDidMount = () => {
    document.onscroll = () =>  {
        var light_pos = document.getElementById('mobileApp').offsetTop;
        var light_height = document.getElementById('mobileApp').offsetHeight;
        var menu_pos = $('#text').offset().top;

        var scroll = window.scrollY;

        if (menu_pos > light_pos && menu_pos < (light_pos + light_height)) {
            document.getElementById('text').classList.add('colorRed')
            /*set state here */
        }
        if((scroll === 0) || (scroll > 1350)) {
            document.getElementById('text').classList.remove('colorRed')
            /*set state here */
        }
    }
}

0 个答案:

没有答案