fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Have to bind routes.match in memoizer #249

Closed morgs32 closed 4 years ago

morgs32 commented 6 years ago

The this in the match function is undefined unless I do

const matchSelector = memoizeOne(routes.match.bind(routes))

And the reason I'm having to use the memoizer is because match returns a new object every time it runs, which will cause the component to render on every state change.

export const mapStateToProps = () => {

  console.log('routes.match', routes.match);
  const matchSelector = memoizeOne(routes.match.bind(routes))
// const repoSelector = createResourceSelector()
// return (state, props) => {
//
//   const {
//     router,
//   } = props
//
//   const match = matchSelector(router.asPath)
//

  const repoSelector = createResourceSelector()
  return (state, props) => {

    const {
      router,
    } = props

    console.log('routes', routes);

    console.log('router.asPath', router.asPath);
    console.log('matchSelector(router.asPath)', matchSelector(router.asPath));
    const match = routes.match(router.asPath)

    return {
      match,
      repo: repoSelector(state, {
        id: props.router.query.repoId,
        type: 'repos',
      })
    }
  }
}