useSelectedLayoutSegment -> useSelectedLayoutSegments, add useSelectedLayoutSegment with previous behavior (#41693)

This commit is contained in:
Tim Neutkens 2022-10-24 03:48:01 +02:00 committed by GitHub
parent 6687f280cf
commit 4f9e3bf0c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 14 deletions

View file

@ -140,11 +140,26 @@ function getSelectedLayoutSegmentPath(
// TODO-APP: Expand description when the docs are written for it. // TODO-APP: Expand description when the docs are written for it.
/** /**
* Get the canonical segment path from this level to the leaf node. * Get the canonical segment path from the current level to the leaf node.
*/ */
export function useSelectedLayoutSegment( export function useSelectedLayoutSegments(
parallelRouteKey: string = 'children' parallelRouteKey: string = 'children'
): string[] { ): string[] {
const { tree } = useContext(LayoutRouterContext) const { tree } = useContext(LayoutRouterContext)
return getSelectedLayoutSegmentPath(tree, parallelRouteKey) return getSelectedLayoutSegmentPath(tree, parallelRouteKey)
} }
// TODO-APP: Expand description when the docs are written for it.
/**
* Get the segment below the current level
*/
export function useSelectedLayoutSegment(
parallelRouteKey: string = 'children'
): string {
const selectedLayoutSegments = useSelectedLayoutSegments(parallelRouteKey)
if (selectedLayoutSegments.length === 0) {
throw new Error('No selected layout segment below the current level')
}
return selectedLayoutSegments[0]
}

View file

@ -2,10 +2,10 @@
// TODO-APP: support typing for useSelectedLayoutSegment // TODO-APP: support typing for useSelectedLayoutSegment
// @ts-ignore // @ts-ignore
import { useSelectedLayoutSegment } from 'next/navigation' import { useSelectedLayoutSegments } from 'next/navigation'
export default function Layout({ children }: { children: React.ReactNode }) { export default function Layout({ children }: { children: React.ReactNode }) {
// useSelectedLayoutSegment should not be thrown // useSelectedLayoutSegment should not be thrown
useSelectedLayoutSegment() useSelectedLayoutSegments()
return children return children
} }

View file

@ -1,9 +1,9 @@
'use client' 'use client'
import { useSelectedLayoutSegment } from 'next/navigation' import { useSelectedLayoutSegments } from 'next/navigation'
export default function Page() { export default function Page() {
const selectedLayoutSegment = useSelectedLayoutSegment() const selectedLayoutSegment = useSelectedLayoutSegments()
return ( return (
<p id="page-layout-segments">{JSON.stringify(selectedLayoutSegment)}</p> <p id="page-layout-segments">{JSON.stringify(selectedLayoutSegment)}</p>

View file

@ -1,9 +1,9 @@
'use client' 'use client'
import { useSelectedLayoutSegment } from 'next/navigation' import { useSelectedLayoutSegments } from 'next/navigation'
export default function Layout({ children }) { export default function Layout({ children }) {
const selectedLayoutSegment = useSelectedLayoutSegment() const selectedLayoutSegment = useSelectedLayoutSegments()
return ( return (
<> <>

View file

@ -1,9 +1,9 @@
'use client' 'use client'
import { useSelectedLayoutSegment } from 'next/navigation' import { useSelectedLayoutSegments } from 'next/navigation'
export default function Layout({ children }) { export default function Layout({ children }) {
const selectedLayoutSegment = useSelectedLayoutSegment() const selectedLayoutSegment = useSelectedLayoutSegments()
return ( return (
<> <>

View file

@ -1,10 +1,10 @@
'use client' 'use client'
import { TabNavItem } from './TabNavItem' import { TabNavItem } from './TabNavItem'
import { useSelectedLayoutSegment } from 'next/navigation' import { useSelectedLayoutSegments } from 'next/navigation'
const CategoryNav = ({ categories }) => { const CategoryNav = ({ categories }) => {
const selectedLayoutSegment = useSelectedLayoutSegment() const selectedLayoutSegment = useSelectedLayoutSegments()
return ( return (
<div style={{ display: 'flex' }}> <div style={{ display: 'flex' }}>

View file

@ -1,10 +1,10 @@
'use client' 'use client'
import { TabNavItem } from '../TabNavItem' import { TabNavItem } from '../TabNavItem'
import { useSelectedLayoutSegment } from 'next/navigation' import { useSelectedLayoutSegments } from 'next/navigation'
const SubCategoryNav = ({ category }) => { const SubCategoryNav = ({ category }) => {
const selectedLayoutSegment = useSelectedLayoutSegment() const selectedLayoutSegment = useSelectedLayoutSegments()
return ( return (
<div style={{ display: 'flex' }}> <div style={{ display: 'flex' }}>