遇到一个比较奇怪的问题, 程序大致逻辑如下
export default class MyAwesomeComponent extends React.Component{
constructor(props){
super(props)
this.handleClick = this.handleClick.bind(this)
}
handleClick(){
//do some expression
window.location = 'url I want to jump'
}
render(){
return <button onClick={this.handleClick}/>
}
}
注意到handleClick
里的window.location
, 没有被正确执行。
改成
handleClick(){
setTimeout(()=>window.location = 'url I want to jump')
}
这里应该是通过setTimeout解决单线程问题。
问题是,为什么会出现单线程的时序问题?老司机能解释吗?
ps: 实际问题是运行在定制化的react、redux环境中的,不排除是定制化的代码部分锁着了window.location
thx