Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2774 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import { useHistory, useLocation } from 'react-router-dom'

const useSearchParams = () => {
  const { search, pathname } = useLocation()
  const history = useHistory()
  const params = new URLSearchParams(search)

  const changeParams = (key, value) => {
    value ? params.set(key, value) : params.delete(key)
    history.replace(`${pathname}?${params.toString()}`)
  }

  const getParams = () => {
    const result = {}
    for (const [key, value] of params.entries()) {
      result[key] = value
    }
    return result
  }

  return {
    setParams: changeParams,
    params: getParams() || {}
  }
}

export default useSearchParams