Reputation: 399
For some reason I used jquery in my react's componentDidMount, but can't use this.setState, because this has been changed by jquery method, I set this to somewhere else, I can accept thisComponent.props.something but why setState failed?
componentDidMount() {
const thisComponent = this //jquery changed `this`, thus set `this` set to somewhere else
$('#container').on('scroll', function() {
console.log(thisComponent.props.something) //work
thisComponent.setState({ // not working
myTestingState: true
})
})
}
There's no error, I tried print out {this.state.myTestingState.toString()} in my render method, it's just not been set.
Upvotes: 2
Views: 47
Reputation: 46491
You can try this
componentDidMount() {
$('#container').on('scroll',() => {
this.setState({
myTestingState: true
})
})
}
Upvotes: 1