最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - debounce and react window resize this reference issue - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

Bind 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
    })
  }
}
发布评论

评论列表(0)

  1. 暂无评论