【发布时间】:2020-01-06 23:41:20
【问题描述】:
当调用调整大小事件时,我正在记录 ref 的当前值。当窗口调整大小时,参考的电流为空。
class Popout extends React.PureComponent {
constructor(props) {
super(props)
this.popoutRef = React.createRef() // current = null
}
isWithinViewPort = () => {
console.log('popoutRef', this.popoutRef)
}
componentDidMount() {
window.addEventListener('resize', this.isWithinViewPort)
}
componentWillUnmount() {
window.removeEventListener('resize', this.isWithinViewPort)
}
render() {
const { id, children, borderColor, style, hidden, pointerLeft, pointerRight } = this.props
if (hidden) {
return null
}
return (
<div id={id} className="popout" style={style} ref={this.popoutRef}>
{children}
</div>
)
}
}
Here's codesandbox link 代码在./components/Popout.js 第 46 行下。
【问题讨论】:
标签: javascript reactjs