路由钩子执行了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>价格&yen;22&nbsp;</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)


0
0

Jokcy

2019-07-30

能描述一下出现这个问题的具体代码吗?有代码我才好帮你排查

0
2
Jokcy
回复
慕仰4782347
额。。。这代码看起来太累了,给个git地址吧,还有如何复现的过程
2019-08-01
共2条回复

全栈进阶课程 React16.8+Next.js+Koa2一步到位开发Github

学习React/Next.js服务端渲染SSR同构设计方案,理解OAuth登录体系的实现原理

651 学习 · 311 问题

查看课程