I am using react
and lodash's debounce
method. The issue I am having is updating state when the user resizes the window.
The issue I am having is that this
is referring to the window
as opposed to the ponent when the user resizes the window in this function:
window.addEventListener('resize', this.delayedCallback)
I have tried setting const that = this
etc. but cannot get the correct scope. Does anyone know how to resolve this issue?
See code below:
class Card extends Component {
constructor(props) {
super(props)
this.state = {
cardElWidth: null
}
this.delayedCallback = debounce(this.setCardWidth, 1000);
this.CardEl = React.createRef()
}
ponentDidMount () {
this.setCardWidth()
window.addEventListener('resize', this.delayedCallback)
}
setPlayerCardWidth() {
this.setState({
cardElWidth: this.CardEl.current.offsetWidth
})
} ... rest of code
I am using react
and lodash's debounce
method. The issue I am having is updating state when the user resizes the window.
The issue I am having is that this
is referring to the window
as opposed to the ponent when the user resizes the window in this function:
window.addEventListener('resize', this.delayedCallback)
I have tried setting const that = this
etc. but cannot get the correct scope. Does anyone know how to resolve this issue?
See code below:
class Card extends Component {
constructor(props) {
super(props)
this.state = {
cardElWidth: null
}
this.delayedCallback = debounce(this.setCardWidth, 1000);
this.CardEl = React.createRef()
}
ponentDidMount () {
this.setCardWidth()
window.addEventListener('resize', this.delayedCallback)
}
setPlayerCardWidth() {
this.setState({
cardElWidth: this.CardEl.current.offsetWidth
})
} ... rest of code
Share
Improve this question
asked Oct 19, 2018 at 11:08
peter flanaganpeter flanagan
9,85027 gold badges82 silver badges140 bronze badges
1 Answer
Reset to default 5Bind the setCardWidth
method to this
in the constructor:
constructor(props) {
super(props)
this.state = {
cardElWidth: null
}
this.setCardWidth = this.setCardWidth.bind(this)
this.delayedCallback = debounce(this.setCardWidth, 1000)
this.CardEl = React.createRef()
}
Or even shorter by binding directly in the debounce expression:
constructor(props) {
super(props)
this.state = {
cardElWidth: null
}
this.delayedCallback = debounce(this.setCardWidth.bind(this), 1000)
this.CardEl = React.createRef()
}
Instead of using bind in the constructor, you can convert the setCardWidth
to a class property, and use an arrow function to automatically bind to this
.
Note: this requires babel's plugin-proposal-class-properties.
setPlayerCardWidth = () => {
this.setState({
cardElWidth: this.CardEl.current.offsetWidth
})
}
If you use class properties, you can remove the constructor as well:
class Card extends Component {
state = {
cardElWidth: null
}
CardEl = React.createRef()
ponentDidMount() {
this.setCardWidth()
window.addEventListener('resize', this.delayedCallback)
}
ponentWillUnmount() {
window.removeEventListener('resize', this.delayedCallback)
}
delayedCallback = debounce(this.setCardWidth, 1000)
setPlayerCardWidth = () => {
this.setState({
cardElWidth: this.CardEl.current.offsetWidth
})
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745217138a4617078.html
评论列表(0条)