Skip to content

Instantly share code, notes, and snippets.

@vuongtran
Created May 14, 2018 16:01
Show Gist options
  • Save vuongtran/c4f76f6340fcaaf050ef2c4ebc128598 to your computer and use it in GitHub Desktop.
Save vuongtran/c4f76f6340fcaaf050ef2c4ebc128598 to your computer and use it in GitHub Desktop.
React handle event window resize
import React, { Component } from 'react';
export default class RenderProducts extends Component {
constructor(props) {
super(props);
this.state = {
innerWidth: window.innerWidth,
}
this.handleResize = this.handelResize.bind(this);
}
handleResize(){
this.setState({innerWidth: window.innerWidth});
}
renderStrategy(){
if(this.state.innerWidth >= 1024) {
return 5;
} else if(this.state.innerWidth >= 1600){
return 6;
}
}
componentDidMount() {
window.addEventListener("resize", this.handleResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.handleResize);
}
render() {
const { products } = this.props;
const num = this.renderStrategy();
if(products && products.length === 0) {
return <div/>;
}
return(
<div>
//TODO render products with num item
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment