Accessing the URL in Server Components #50405
Replies: 4 comments 6 replies
-
|
Mmm my spontaneous take, would be, prop drilling yeah, but you can try (not always possible) to switch to composition, by identifying which components actually consume the data, and allow their containers to use I'd post some snippet, but this video explains the technique well: https://www.youtube.com/watch?v=3XaXKiXtNjw SnippetThis is a silly example of course, made to work. It might not be always easy/possible to do: Say you have: import { useState } from "react";
type User = {
name: string;
};
export function Dashboard() {
const [user, setUser] = useState<User | null>(null);
return (
<div>
{user ? (
<UserProfile user={user} />
) : (
<LoginForm onLogin={() => setUser({ name: "joseph" })} />
)}
</div>
);
}
const UserProfile = ({ user }: { user: User }) => {
return (
<div>
<h2>Profile</h2>
<Nav />
<ProfileCard user={user} />
</div>
);
};
const LoginForm = ({ onLogin }: { onLogin: () => void }) => {
return <button onClick={onLogin}>Login</button>;
};
/**
* Dashboard blocks
*/
const Nav = () => (
<nav>
<ul>
<li>
<a href="/">Home</a>
</li>
</ul>
</nav>
);
const ProfileCard = ({ user }: { user: User }) => {
return (
<article>
<h3>Welcome</h3>
<UserName user={user} />
</article>
);
};
const UserName = ({ user }: { user: User }) => {
return <h4>{user.name}</h4>;
};You could refactor, with "composition" into: import { useState, type ReactNode } from "react";
type User = {
name: string;
};
export function Dashboard() {
const [user, setUser] = useState<User | null>(null);
return (
<div>
{user ? (
<UserProfile>
<ProfileCard>
<UserName user={user} />
</ProfileCard>
</UserProfile>
) : (
<LoginForm onLogin={() => setUser({ name: "joseph" })} />
)}
</div>
);
}
const UserProfile = ({ children }: { children: ReactNode }) => {
return (
<div>
<h2>Profile</h2>
<Nav />
{children}
</div>
);
};
const LoginForm = ({ onLogin }: { onLogin: () => void }) => {
return <button onClick={onLogin}>Login</button>;
};
/**
* Dashboard blocks
*/
const Nav = () => (
<nav>
<ul>
<li>
<a href="/">Home</a>
</li>
</ul>
</nav>
);
const ProfileCard = ({ children }: { children: ReactNode }) => {
return (
<article>
<h3>Welcome</h3>
{children}
</article>
);
};
const UserName = ({ user }: { user: User }) => {
return <h4>{user.name}</h4>;
}; |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for your answer. Interesting approach, but for slightly more complex applications, like in my case (the example above was just a simple illustration), I don't think I'll get away with it. Different approach: |
Beta Was this translation helpful? Give feedback.
-
|
Hey mate! Somehow the innovations of 13.4. not quite mature yet. Functionally not yet complete, it seems to me. |
Beta Was this translation helpful? Give feedback.
-
|
Hi @icyJoseph, I have some feedback and real-time usage now. Unfortunately it doesn't work as it should, with simultaneous access I get the URLs of other users :/ |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
my application is connected to a CMS.
The data is loaded in page.js via the current URL.
Example URL: /summer/sports/golf
On some (server) components I need access to the data again. Fetching the data again would not be a problem, since Next.js solves this intelligently (deduping).
But I don't have access to the current URL(and params) in (sub)components.
I have a catch all route for this ( [...pagename] ).
What's the best way to solve this? I am currently passing on the data via props ("props drilling").
Example:
In
<SocialMedia/>I only have access to the data if I loop through the data from page.js via props, since I have no opportunity to access the current URL in<SocialMedia/>.How can I solve this?
Beta Was this translation helpful? Give feedback.
All reactions