出于某种原因,我在我的react的componentDidMount中使用了jquery,但是不能使用this.setState,因为这个已经被jquery方法改变了,我把它设置到别的地方,我可以接受thisComponent.props.something但是为什么setState失败了?
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
})
})
}
没有错误,我尝试在我的渲染方法中打印出 {this.state.myTestingState.toString()},只是没有设置。
最佳答案
你可以试试这个
componentDidMount() {
$('#container').on('scroll',() => {
this.setState({
myTestingState: true
})
})
}
关于javascript - 更改时的 setState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44916568/