Created
May 14, 2018 16:01
-
-
Save vuongtran/c4f76f6340fcaaf050ef2c4ebc128598 to your computer and use it in GitHub Desktop.
React handle event window resize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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