Skip to content

Commit

Permalink
Replace useDelayedNavigate with useTransitionNavigate
Browse files Browse the repository at this point in the history
  • Loading branch information
theninthsky committed Aug 23, 2023
1 parent bf08045 commit 426b7f6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
- [Meta](#meta)
- [React Hooks](#react-hooks)
- [useAxios](#useaxios)
- [useDelayedNavigate](#usedelayednavigate)
- [useFetch](#usefetch)
- [useMedia](#usemedia)
- [useObjectState](#useobjectstate)
- [usePersistedState](#usepersistedstate)
- [usePersistedObjectState](#usepersistedobjectstate)
- [useProgressiveImage](#useprogressiveimage)
- [useTransitionNavigate](#usetransitionnavigate)
- [Utility Functions](#utility-functions)
- [lazyPrefetch](#lazyprefetch)
<br>
Expand Down Expand Up @@ -165,24 +165,6 @@ import { axios } from 'frontend-essentials'
axios.defaults.withCredentials = true
```

## useDelayedNavigate

Delays React Router's navigation until the target component is rendered:

```js
const navigate = useDelayedNavigate()

<NavLink
to='home'
onClick={event => {
event.preventDefault()
navigate('home')
}}
>
Home
</NavLink>
```

## useFetch

Similar to useAxios but with native fetch's API:
Expand Down Expand Up @@ -279,6 +261,24 @@ const [src, blur] = useProgressiveImage(lowQualitySrc, highQualitySrc)
return <img style={{ filter: blur ? 'blur(20px)' : 'none' }} src={src} />
```

## useTransitionNavigate

Delays React Router's navigation until the target page is rendered:

```js
const navigate = useTransitionNavigate()

<NavLink
to='home'
onClick={event => {
event.preventDefault()
navigate('home')
}}
>
Home
</NavLink>
```

<br>

# Utility Functions
Expand Down
11 changes: 0 additions & 11 deletions src/hooks/useDelayedNavigate.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/hooks/useTransitionNavigate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useTransition } from 'react'
import { useNavigate, To, NavigateOptions } from 'react-router-dom'

const useTransitionNavigate = () => {
const [, startTransition] = useTransition()
const navigate = useNavigate()

return (to: To, options?: NavigateOptions) => startTransition(() => navigate(to, options))
}

export default useTransitionNavigate
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ export { default as Media, MediaProps } from './components/Media'
export { default as Meta, MetaProps } from './components/Meta'

export { default as useAxios, axios, UseAxiosRequestConfig, UseAxiosResponse } from './hooks/useAxios'
export { default as useDelayedNavigate } from './hooks/useDelayedNavigate'
export { default as useFetch, UseFetchRequestConfig, UseFetchResponse } from './hooks/useFetch'
export { default as useMedia, QueriesMap } from './hooks/useMedia'
export { default as useObjectState } from './hooks/useObjectState'
export { default as usePersistedObjectState } from './hooks/usePersistedObjectState'
export { default as usePersistedState, PersistenceOptions } from './hooks/usePersistedState'
export { default as useProgressiveImage } from './hooks/useProgressiveImage'
export { default as useTransitionNavigate } from './hooks/useTransitionNavigate'

export { default as camelCasedKeys } from './utils/camel-cased-keys'
export { default as lazyPrefetch } from './utils/lazy-prefetch'

0 comments on commit 426b7f6

Please sign in to comment.