Replies: 9 comments 21 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>;
}; |
|
|
|
by the way. if you are using next-intl, you can get locale by using import { useLocale } from "next-intl"; |
|
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>
} |
|
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 |
|
Any update on that ? Quite important to me to get dynamic url segment inside server components without deep props drilling :/ |
|
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'); |
|
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
} |
|
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. |
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
layoutandpagethrough theparamsprop. 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
All reactions