Replies: 9 comments 22 replies
-
Hi, This is in the docs, and the solution is to just use JavaScript.
Though it is not very easily found I guess. So you could do for example: // app/store.tsx
const createStore = () => {
let data: string | null = null;
const setData = (next: string) => {
data = next;
};
const getData = () => {
return data;
};
return { setData, getData };
};
export const store = createStore(); And then at say: // app/[foo]/[bar]/page.tsx
import { Outer } from "../../Outer";
import { store } from "../../store";
export default function Page({
params,
}: {
params: { foo: string; bar: string };
}) {
const { foo, bar } = params;
store.setData(`/${foo}/${bar}`);
return <Outer />;
} And then you'd have: import { Inner } from "./Inner";
export const Outer = () => {
return <Inner />;
}; Where, finally, Inner, just reads the data: import { store } from "./store";
export const Inner = () => {
return <div>{store.getData()}</div>;
}; |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
by the way. if you are using next-intl, you can get locale by using import { useLocale } from "next-intl"; |
Beta Was this translation helpful? Give feedback.
-
Hey everybody, just built a library to handle this problem: How to use: /app/[myParam]/page.tsx import { pageContext } from '@sodefa/next-server-context'
import { NestedServerComponent } from './NestedServerComponent'
export default pageContext.Wrapper(({ params, searchParams }) => {
return (
<>
<h1>MyPage</h1>
<NestedServerComponent />
</>
)
}) /app/[myParam]/NestedServerComponent.tsx import { pageContext } from '@sodefa/next-server-context'
export const NestedServerComponent = () => {
const { params, searchParams } = pageContext.getOrThrow()
return <div>NestedServerComponent: {params.myParam}</div>
} |
Beta Was this translation helpful? Give feedback.
-
Is there a native Next.js solution for this yet? If not, what might be the reason it hasn't been done? Feels like a pretty common problem in my mind |
Beta Was this translation helpful? Give feedback.
-
Any update on that ? Quite important to me to get dynamic url segment inside server components without deep props drilling :/ |
Beta Was this translation helpful? Give feedback.
-
A better approach I see is to use headers in middleware: // middleware.js
const res = NextResponse.redirect(url);
res.headers.set('x-store', storeCode);
return res // server side
const headerModule = require('next/headers');
return headerModule.headers().get('x-store'); |
Beta Was this translation helpful? Give feedback.
-
I prefer using cookies in middleware.ts and then get value of it there for themes and local as well. but it can be used as you need. // middleware.ts
export function middleware(request: NextRequest) {
const response = NextResponse.next();
response.cookies.set('lang',getLocalByRoute );
return response
}; and in SSC import { cookies } from "next/headers";
export default function ButtonWithTranslations() {
const lang = cookies().get("lang")?.value || "en";
// rest code
} |
Beta Was this translation helpful? Give feedback.
-
I'm trying so hard to be an optimist about this whole "server components" thing, and then I crash into a problem so dumb and obvious… I absolutely cannot believe the App Router got pushed out the door without this sort of basic functionality. Every solution I've seen on this page makes sense but is ultimately a creaky hack over something that is just fundamental to the very concept of a server component. Augh. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
With the new app router, dynamic parameters are provided to certain server components like
layout
andpage
through theparams
prop. For other server components used inside a layout or page, it appears that it is necessary to pass any dynamic params as props, since context doesn't work for server components.I am also aware that there is a useParams hook, but that only works for client components.
Am I missing something here? Is there a cleaner way to provide dynamic params (or search params) to nested server components in a page? Is prop drilling the only option for now?
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions