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

RootLayout doesn't receive searchParams as props #43863

Closed
1 task done
mpereira opened this issue Dec 8, 2022 · 12 comments
Closed
1 task done

RootLayout doesn't receive searchParams as props #43863

mpereira opened this issue Dec 8, 2022 · 12 comments
Labels
bug Issue was opened via the bug report template.

Comments

@mpereira
Copy link

mpereira commented Dec 8, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:54 PDT 2022; root:xnu-8792.41.9~2/RELEASE_X86_64
    Binaries:
      Node: 16.15.0
      npm: 8.5.5
      Yarn: 1.22.18
      pnpm: N/A
    Relevant packages:
      next: 13.0.6
      eslint-config-next: 13.0.4
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue

https://stackblitz.com/edit/vercel-next-js-z5qkbs?file=app%2Flayout.js

To Reproduce

  1. Open https://stackblitz.com/edit/vercel-next-js-z5qkbs?file=app/layout.js

  2. Open https://vercelnextjsz5qkbs-4a2j--3000.local-credentialless.webcontainer.io/?foo=true

  3. Check terminal output in https://stackblitz.com/edit/vercel-next-js-z5qkbs?file=app/layout.js (from console.log(props) in RootLayout

    {
      children: {
        '$$typeof': Symbol(react.element),
        type: {
          '$$typeof': Symbol(react.module.reference),
          filepath: '/home/projects/vercel-next-js-z5qkbs/node_modules/next/dist/client/components/layout-router.js',
          name: '',
          async: false
        },
        key: null,
        ref: null,
        props: {
          parallelRouterKey: 'children',
          segmentPath: [Array],
          error: undefined,
          errorStyles: undefined,
          loading: undefined,
          loadingStyles: undefined,
          hasLoading: false,
          template: [Object],
          templateStyles: undefined,
          notFound: [Object],
          notFoundStyles: undefined,
          childProp: [Object],
          rootLayoutIncluded: true
        },
        _owner: null,
        _store: {}
      },
      params: {}
    }
  4. props in RootLayout doesn't include searchParams, even though layouts should be considered server components.

Describe the Bug

props in RootLayout doesn't include searchParams, even though layouts should be considered server components.

Expected Behavior

props in RootLayout shoud include searchParams.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@mpereira mpereira added the bug Issue was opened via the bug report template. label Dec 8, 2022
@filipesmedeiros
Copy link

I think only pages receive searchParams. Don't know if that is by design or not

@Fredkiss3
Copy link
Contributor

Fredkiss3 commented Dec 8, 2022

@filipesmedeiros I think that’s by design since layouts are supposed not to rerender on page transitions.

and in the generated types for next layouts, I have these types here :

type PageParams = Record<string, string>
interface PageProps {
  params: any
  searchParams?: any
}
interface LayoutProps {
  children: React.ReactNode
  params: any
}

It seems like Layouts cannot receive searchParams.

PS: I got these types after running next build and going to .next/types/app/layout.ts

@mpereira
Copy link
Author

mpereira commented Dec 8, 2022

For context, my use case is conditionally rendering some parts of the layout depending on URL search params.

@Fredkiss3
Copy link
Contributor

Can you use a client component to conditionnaly render the components ?

because in client components wherever you are in the tree, you can call useSearchParams to get the URL search params.

And since you can pass server components as children (or props) to client components, you can even pre-render the parts you need on the layout and pass them as props to a client component with useSearchParams in it.

@mpereira
Copy link
Author

mpereira commented Dec 8, 2022

Thanks for the suggestion, @Fredkiss3. That would work.

@karlhorky
Copy link
Contributor

Also suggested this change to client component + useSearchParams in the next-pokedex example from @chantastic's react.holiday series last year, where @chantastic also ran into problems expecting searchParams to be passed to the layout:

@karlhorky
Copy link
Contributor

@mpereira to be clear, this affects all layouts, correct? Not only RootLayouts, but also nested layouts?

If so, maybe it makes sense to update the title + description + demo of the issue?

It's nice to have a tracking issue for this, since I remember seeing somewhere "the Next.js team is considering adding additional information to Layout props" - can't find the resource right now though...

@karlhorky
Copy link
Contributor

Or wait, I just found this issue as well, sounds similar:

@balazsorban44 would you suggest marking #43863 as a duplicate of #43704?

@mpereira
Copy link
Author

@karlhorky marking this one as a duplicate sounds good to me.

@karlhorky
Copy link
Contributor

Ok, I asked @adileo if he would edit the issue to mention the additional details explicitly and create a demo, potentially using yours as a starting point: #43704 (comment)

@balazsorban44
Copy link
Member

Thanks for the ping and sorry for the late reply, I think we can fold this discussion into #43704 as suggested above. 👍

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

5 participants