connected-next-router icon indicating copy to clipboard operation
connected-next-router copied to clipboard

search.replace is not a function

Open koraysels opened this issue 3 years ago • 3 comments

Replace method is not working .

Expected is for this to work as it does in connected-react-router

 const state = store.getState()
    store.dispatch(
      replace({
        pathname: state.router.location.pathname,
        search: {
          ...state.router.location.search,
          ...action.pushToUrl
        }
      })
    )

but it throws an error

Uncaught TypeError: search.replace is not a function
    at formatUrl (format-url.js?e83e:22)
    at formatWithValidation (utils.js?83fd:5)
    at resolveHref (router.js?7a5c:24)
    at prepareUrlAs (router.js?7a5c:29)

koraysels avatar Sep 13 '21 15:09 koraysels

@koraysels How does the state.router.location object look like, can you log it?

danielr18 avatar Sep 13 '21 15:09 danielr18

yeah sure.

like this:

router: {
    location: {
      href: '/post/verhaal',
      pathname: '/post/verhaal',
      search: '',
      hash: ''
    }
  }

and if I add some search params in the url manually from the URL bar in chrome the search parameters get synchronised properly..

router: {
    location: {
      href: '/post/eerste-verhaal?thema=21',
      pathname: '/post/eerste-verhaal',
      search: '?theme=21',
      hash: ''
    }
  }

koraysels avatar Sep 14 '21 07:09 koraysels

So after some digging around. I found that if I use query instead of search. it does kinda work but it keeps refreshing the state by triggering @@router/LOCATION_CHANGE which is very annoying .. i do not understand. is something wrong with my middleware perhaps ?

import {replace} from 'connected-next-router'

const urlSync = (store) => (next) => (action) => {
  if (action.pushToUrl) {
    const state = store.getState()

    store.dispatch(
      replace({
        pathname: state.router.location.pathname,
        query: {
          ...action.pushToUrl
        }
      })
    )
  }

  return next(action)
}

export default urlSync

koraysels avatar Sep 14 '21 08:09 koraysels