Javascript 使用JS函数捕捉浏览器';s窗口大小调整不是';行不通
这是我的组件代码。除此“onResize”功能外,所有功能均已成功运行Javascript 使用JS函数捕捉浏览器';s窗口大小调整不是';行不通,javascript,reactjs,Javascript,Reactjs,这是我的组件代码。除此“onResize”功能外,所有功能均已成功运行 class MyComponent extends Component { constructor(props) { super(props) this.onResize = this.onResize.bind(this) } onResize = () => { window.onresize = () => {
class MyComponent extends Component {
constructor(props) {
super(props)
this.onResize = this.onResize.bind(this)
}
onResize = () => {
window.onresize = () => {
alert(true)
//I had tried with 'this.alert(true)' too
}
}
}
componentDidMount() {
this.onResize()
}
//rest of the code ommited
您必须在
componentDidMount
生命周期中添加事件侦听器:
componentDidMount() {
window.addEventListener('resize', this.handleWindowResize)
}
请记住在卸载组件时删除事件侦听器,以避免内存泄漏:
componentWillUnmount() {
window.removeEventListener('resize', this.handleWindowResize)
}
您必须在
componentDidMount
生命周期中添加事件侦听器:
componentDidMount() {
window.addEventListener('resize', this.handleWindowResize)
}
请记住在卸载组件时删除事件侦听器,以避免内存泄漏:
componentWillUnmount() {
window.removeEventListener('resize', this.handleWindowResize)
}
您需要为调整大小添加一个事件侦听器
onResize() {
this.setState({
width: $(window).width(),
height: $(window).height()
})
}
componentWillMount() {
this.onResize();
}
componentDidMount() {
window.addEventListener("resize", this.onResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.onResize);
}
您需要为调整大小添加一个事件侦听器
onResize() {
this.setState({
width: $(window).width(),
height: $(window).height()
})
}
componentWillMount() {
this.onResize();
}
componentDidMount() {
window.addEventListener("resize", this.onResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.onResize);
}