Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2614 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1548 stevensc 1
import { useHistory, useLocation } from 'react-router-dom'
2
 
3
const useSearchParams = () => {
4
  const { search, pathname } = useLocation()
5
  const history = useHistory()
6
  const params = new URLSearchParams(search)
7
 
8
  const changeParams = (key, value) => {
9
    value ? params.set(key, value) : params.delete(key)
10
    history.replace(`${pathname}?${params.toString()}`)
11
  }
12
 
13
  const getParams = () => {
14
    const result = {}
15
    for (const [key, value] of params.entries()) {
16
      result[key] = value
17
    }
18
    return result
19
  }
20
 
21
  return {
22
    setParams: changeParams,
23
    params: getParams() || {}
24
  }
25
}
26
 
27
export default useSearchParams