0

for some reason onSlideClick (callback) is returning the right result on the second click ... when I replaced the spread operator with push() it worked. why does this function adds index to slidesIndex only in the second click ?

handleClick = (index) => {
  const { onSlideClick } = this.props
  const { slidesIndex } = this.state
  if (!slidesIndex.includes(slidesIndex[index])) {
    this.setState({
      slidesIndex: [...slidesIndex, index]
    })
    console.log('add')
  }
  onSlideClick(slidesIndex)
}
abdelhak.ajbouni
  • 170
  • 1
  • 10
  • while using push, it changes the value at the existing reference, whereas using spread, it creates a new copy of the object and hence slidesIndex isn't giving the correct response – Shubham Khatri Sep 19 '18 at 09:32
  • this.setState(prevState => ({ slidesIndex: [...prevState.slidesIndex, index] })) – Ricardo Costa Sep 19 '18 at 09:50
  • By the way, the reason why your second click onSlideClick returning the right result is the first click onSlideClick can't get the slidesIndex you setState unless you give it directly. – R.hui Sep 19 '18 at 10:05

0 Answers0