Last active
May 21, 2019 08:26
-
-
Save RayLuxembourg/e547adadcd646efd29eeae202a233d0c to your computer and use it in GitHub Desktop.
bind argument without effecting this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PoseSwiper extends Component<Props> { | |
state = { | |
activeIndex: 0 | |
}; | |
config = { | |
slidesPerView: 1, | |
observer: true, | |
on: { | |
slideChange: function(applyNewIndex: (index: number) => void) { | |
applyNewIndex(this.activeIndex); | |
} | |
} | |
}; | |
constructor(props) { | |
super(props); | |
const method = this.config.on.slideChange; | |
const onIndexChange = this.setIndex; | |
this.config.on.slideChange = function() { | |
method.bind(this, onIndexChange)(); | |
}; | |
} | |
setIndex = (index: number) => { | |
this.setState((prev) => ({ ...prev, activeIndex: index })); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment