Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoize useParams #56771

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/next/src/client/components/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,15 @@ export function useParams<T extends Params = Params>(): T {
const globalLayoutRouter = useContext(GlobalLayoutRouterContext)
const pathParams = useContext(PathParamsContext)

// When it's under app router
if (globalLayoutRouter) {
return getSelectedParams(globalLayoutRouter.tree) as T
}
return useMemo(() => {
// When it's under app router
if (globalLayoutRouter?.tree) {
return getSelectedParams(globalLayoutRouter.tree) as T
}

// When it's under client side pages router
return pathParams as T
// When it's under client side pages router
return pathParams as T
}, [globalLayoutRouter?.tree, pathParams])
}

// TODO-APP: handle parallel routes
Expand Down
30 changes: 30 additions & 0 deletions test/e2e/app-dir/navigation/app/search-params/[foo]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client'
import { useParams, useRouter } from 'next/navigation'
import { useState } from 'react'
import { useEffect } from 'react'

export default function Page() {
const params = useParams()
const router = useRouter()
const [count, setCount] = useState(0)
useEffect(() => {
console.log('params changed')
}, [params])
return (
<div>
<button
id="rerender-button"
onClick={() => setCount((count) => count + 1)}
>
Re-Render {count}
</button>

<button
id="change-params-button"
onClick={() => router.push('/search-params/bar')}
>
Change Params
</button>
</div>
)
}
48 changes: 48 additions & 0 deletions test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,54 @@ createNextDescribe(
: JSON.stringify(requests)
}, 'success')
})

describe('useParams identity between renders', () => {
async function runTests(page: string) {
const browser = await next.browser(page)

await check(
async () => JSON.stringify(await browser.log()),
/params changed/
)

let outputIndex = (await browser.log()).length

await browser.elementById('rerender-button').click()
await browser.elementById('rerender-button').click()
await browser.elementById('rerender-button').click()

await check(async () => {
return browser.elementById('rerender-button').text()
}, 'Re-Render 3')

await check(async () => {
const logs = await browser.log()
return JSON.stringify(logs.slice(outputIndex)).includes(
'params changed'
)
? 'fail'
: 'success'
}, 'success')

outputIndex = (await browser.log()).length

await browser.elementById('change-params-button').click()

await check(
async () =>
JSON.stringify((await browser.log()).slice(outputIndex)),
/params changed/
)
}

it('should be stable in app', async () => {
await runTests('/search-params/foo')
})

it('should be stable in pages', async () => {
await runTests('/search-params-pages/foo')
})
})
})

describe('hash', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useParams, useRouter } from 'next/navigation'
import { useState } from 'react'
import { useEffect } from 'react'

export default function Page() {
const params = useParams()
const router = useRouter()
const [count, setCount] = useState(0)
useEffect(() => {
console.log('params changed')
}, [params])
return (
<div>
<button
id="rerender-button"
onClick={() => setCount((count) => count + 1)}
>
Re-Render {count}
</button>

<button
id="change-params-button"
onClick={() => router.push('/search-params-pages/bar')}
>
Change Params
</button>
</div>
)
}
Loading