use-location.ts 536 B

123456789101112131415161718192021222324
  1. import { useCallback, useMemo } from 'react'
  2. import useIsMounted from './use-is-mounted'
  3. import { location } from '@/shared/components/location'
  4. export const useLocation = () => {
  5. const isMounted = useIsMounted()
  6. const assign = useCallback(
  7. url => {
  8. if (isMounted.current) {
  9. location.assign(url)
  10. }
  11. },
  12. [isMounted]
  13. )
  14. const reload = useCallback(() => {
  15. if (isMounted.current) {
  16. location.reload()
  17. }
  18. }, [isMounted])
  19. return useMemo(() => ({ assign, reload }), [assign, reload])
  20. }