nextjs-navigation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLink Component Prefetching Behavior
Link组件预取行为
Understanding prefetch Prop Values
理解prefetch属性值
The prop on Next.js components has three distinct behaviors in App Router:
prefetch<Link>prefetch={false}prefetch={false}tsx
<Link href="/dashboard" prefetch={false}>
Dashboard
</Link>prefetch={undefined}prefetch={true}tsx
<Link href="/profile/[id]" prefetch={true}>
User Profile
</Link>Next.js 组件的属性在App Router中有三种不同的行为:
<Link>prefetchprefetch={false}prefetch={false}tsx
<Link href="/dashboard" prefetch={false}>
Dashboard
</Link>prefetch={undefined}prefetch={true}tsx
<Link href="/profile/[id]" prefetch={true}>
User Profile
</Link>Suspense Boundaries and Search Params
Suspense边界与搜索参数
Same-Page Navigation Issue
同页面导航问题
When navigating on the same page with only search parameter changes, Suspense boundaries do not re-trigger their loading states by default. This can create a poor user experience where the UI appears frozen during data refetching.
当仅修改搜索参数进行同页面导航时,Suspense边界默认不会重新触发其加载状态。这可能会导致在数据重新获取期间UI看起来处于冻结状态,带来不佳的用户体验。
Solution: Key-Based Suspense
解决方案:基于Key的Suspense配置
Force Suspense boundaries to show loading states during search param changes by providing a unique prop that includes the changing search parameter:
keytsx
// app/search/page.tsx
export default function SearchPage({
searchParams
}: {
searchParams: { q?: string }
}) {
return (
<Suspense
key={searchParams.q}
fallback={<LoadingSpinner />}
>
<SearchResults query={searchParams.q} />
</Suspense>
)
}The key change ensures React treats the Suspense boundary as a new instance, properly displaying the fallback during the transition.
通过提供包含变化搜索参数的唯一属性,强制Suspense边界在搜索参数变化时显示加载状态:
keytsx
// app/search/page.tsx
export default function SearchPage({
searchParams
}: {
searchParams: { q?: string }
}) {
return (
<Suspense
key={searchParams.q}
fallback={<LoadingSpinner />}
>
<SearchResults query={searchParams.q} />
</Suspense>
)
}该key的变更确保React将Suspense边界视为新实例,在过渡期间正确显示回退内容。
Type Safety
类型安全
Typing Page Props
为页面属性添加类型
Use the helper type to correctly type page component props in App Router:
PagePropstsx
import type { PageProps } from 'next'
export default function Page({ params, searchParams }: PageProps) {
// params and searchParams are properly typed
}This helper provides accurate types for the and objects passed to page components, improving type safety and developer experience.
paramssearchParams使用辅助类型为App Router中的页面组件属性正确添加类型:
PagePropstsx
import type { PageProps } from 'next'
export default function Page({ params, searchParams }: PageProps) {
// params和searchParams已被正确添加类型
}此辅助类型为传递给页面组件的和对象提供准确的类型,提升类型安全性和开发者体验。
paramssearchParamsComponent State Preservation
组件状态保留
cacheComponents Flag
cacheComponents实验性标志
When the experimental flag is enabled, Next.js leverages React's component to maintain component state across client-side navigations:
cacheComponents<Activity>tsx
// next.config.js
module.exports = {
experimental: {
cacheComponents: true
}
}This preserves component state (such as form inputs, scroll position, or local component state) when users navigate away and return, creating a more native app-like experience. The component handles the state preservation automatically without requiring manual state management.
<Activity>当启用实验性标志时,Next.js会利用React的组件在客户端导航期间保持组件状态:
cacheComponents<Activity>tsx
// next.config.js
module.exports = {
experimental: {
cacheComponents: true
}
}这会在用户离开并返回页面时保留组件状态(如表单输入、滚动位置或本地组件状态),创造更接近原生应用的体验。组件会自动处理状态保留,无需手动进行状态管理。
<Activity>