Replies: 1 comment 1 reply
-
|
Hi @iNikAnn You’re trying to use isPending as “router.refresh() loading state”. That’s not what useTransition guarantees. isPending only tracks React work scheduled by that specific keep useTransition inside the hook and return // useRefresh.ts
'use client'
import { useRouter } from 'next/navigation'
import { useCallback, useTransition } from 'react'
export function useRefresh() {
const router = useRouter()
const [isPending, startTransition] = useTransition()
const refresh = useCallback(() => {
startTransition(() => {
router.refresh()
})
}, [router, startTransition])
return { isPending, refresh }
}// Component.tsx
'use client'
import { useRefresh } from './useRefresh'
export default function Component() {
const { isPending, refresh } = useRefresh()
return (
<button onClick={refresh} disabled={isPending}>
{isPending ? 'Refreshing…' : 'Refresh'}
</button>
)
}If you must keep pending in the parent Then don’t rely on useTransition for router.refresh() at all. Use your own boolean: const [refreshing, setRefreshing] = useState(false)
const refresh = async () => {
setRefreshing(true)
router.refresh()
// you need some real “done” signal to setRefreshing(false)
}But be honest: there is no official “refresh finished” event for router.refresh(). If you need a real completion signal, use a server action / fetch + state you control, not router.refresh() as your “async operation”. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I am experiencing an issue with useTransition and router.refresh() in Next.js 15.3.3. When the logic is executed directly inside a component, isPending works as expected. However, when the exact same logic is moved into a custom hook, isPending never becomes true.
Part 1: Working Scenario (Inside Component)
In this case, when I click the button, isPending correctly flips to true while the page is refreshing.
Part 2: Broken Scenario (Inside Custom Hook)
When I move the logic into a hook, even if I pass the router and startTransition from the parent component as arguments, isPending remains false throughout the entire operation.
How can I ensure isPending correctly reflects the router.refresh() state when using custom hooks to handle post-action logic?
Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions