Home > AI > Frontend > ReactJS >

detect resize event

componentDidMount() {
    window.addEventListener('resize', () => {
        this.setState({
            isMobile: window.innerWidth < 1200
        });
    }, false);
}

render() {
    const className = this.state.isMobile ? 'mobile' : '';
    return (
        <Component className={className} />
    )
}

Leave a Reply