路由钩子执行了25次?
来源:6-1 认证和授权介绍

慕仰4782347
2019-07-30
就切换个路由怎么这个路由居然执行了25次?
2回答
-
慕仰4782347
提问者
2019-07-31
// import { Button } from 'antd'
import Router from 'next/router'
// import Link from 'next/link'
// import getConfig from '../next.config'
// const { publicRuntimeConfig } = getConfig()
// import store from '../store/store'
import { connect } from 'react-redux'
import { add }from '../store/store'
const Index = ({counter,username,add,rename}) => {
// function gotoTestB() {
// Router.push(
// {
// pathname: '/test/b',
// query: {
// id: 39
// }
// },
// '/test/b/39'
// )
// }
const events = [
'routeChangeStart',
'routeChangeComplete',
'routeChangeError',
'beforeHistoryChange',
'hashChangeStart',
'hashChangeComplete'
]
function maskEvent(type) {
return (...args) => {
console.log(type, ...args)
}
}
events.forEach(event => {
Router.events.on(event, maskEvent(event))
})
return (
<>
<span>Index</span>
<p>{counter}</p>
<p>{username}</p>
<input value={username} onChange={e=>rename(e.target.value)}/>
<button onClick={()=>add(counter)}>store add</button>
{/* <pre>烦死了副经理是否解决</pre>
<kbd>fjsdlfjs</kbd><br/>
<cite>范德萨发的</cite>
<big>价格¥22 </big>
<tt>测试df</tt> */}
{/* <Link href="/a?id=3">
<Button>button</Button>
</Link>
<Button onClick={gotoTestB}>Go B</Button> */}
{/* <a href={publicRuntimeConfig.OAUTH_URL}>去登录</a> */}
</>
)
}
Index.getInitialProps = async ({reduxStore})=>{
reduxStore.dispatch(add(5))
return {}
}
//connect 里面的方式是帮助我们映射store里面的东西到组件里
export default connect(function mapStateToProps(state){
return {
counter:state.counter.count,
username:state.user.username
}
},function mapDispatchToProps(dispatch){
return {
add:(num)=>dispatch({type:'add',num}),
rename:(name)=>dispatch({type:'update_name',name})
}
})(Index)
00 -
Jokcy
2019-07-30
能描述一下出现这个问题的具体代码吗?有代码我才好帮你排查
022019-08-01
全栈进阶课程 React16.8+Next.js+Koa2一步到位开发Github
651 学习 · 311 问题
相似问题