Understanding SSR / Client components
Answered
Oak rough bulletgall wasp posted this in #help-forum
Oak rough bulletgall waspOP
Hi folks! I have two questions
1 - If I make a component as minimal as possible with a state management as useState, or useSearchParams and I label this component with "use client"; ... If I import that inside a component which haven't been declared with "use client" ... What happens then ? does the whole component become client component or is it able to differentiate those two, and build the one component SSR, and then filling out the client component ?
2 - What about Redux ? Since the application i intent to build will be somewhat complexed with many nested routes .. I really want to ensure an easy state management... but will redux be anti next.js pattern due to the SSR and Client Component setup which Next.js leverage as? 🙂
Thanks for taking yoru time to answer! 😄
1 - If I make a component as minimal as possible with a state management as useState, or useSearchParams and I label this component with "use client"; ... If I import that inside a component which haven't been declared with "use client" ... What happens then ? does the whole component become client component or is it able to differentiate those two, and build the one component SSR, and then filling out the client component ?
2 - What about Redux ? Since the application i intent to build will be somewhat complexed with many nested routes .. I really want to ensure an easy state management... but will redux be anti next.js pattern due to the SSR and Client Component setup which Next.js leverage as? 🙂
Thanks for taking yoru time to answer! 😄
Answered by Dayo
1. if you import a client component into a server component, it stays the same. your client component remains a client component while the other parts of your server component remain the same.
the idea for marking specific parts of your app as client and leaving the rest unmarked is so that you ship the least possible amount of client-side Javascript which helps to improve performance.
for example, in this layout file below, rather than make the whole layout a Client Component, move the interactive logic to a Client Component (e.g. <SearchBar />) and keep your layout as a Server Component. this means you don't have to send all the component Javascript of the layout to the client.
as per the last paragraph, both Client components and Server components are actually SSR (server side rendered). see Dan Abramov's explanation here - https://github.com/reactwg/server-components/discussions/4
2. depends. i've been using Context for most of my global Client state management. if you're more comfortable with Redux, why not. it's not an anti-pattern. there are truly things you'd need global client state management for like user authentication.
but for managing data that comes from your server, with Next 13 you can fetch data directly in your Server components using the fetch api, and you can use a tool like react-query for fetching inside your client components.
the idea for marking specific parts of your app as client and leaving the rest unmarked is so that you ship the least possible amount of client-side Javascript which helps to improve performance.
for example, in this layout file below, rather than make the whole layout a Client Component, move the interactive logic to a Client Component (e.g. <SearchBar />) and keep your layout as a Server Component. this means you don't have to send all the component Javascript of the layout to the client.
// SearchBar is a Client Component
import SearchBar from './searchbar'
// Logo is a Server Component
import Logo from './logo'
// Layout is a Server Component by default
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<>
<nav>
<Logo />
<SearchBar />
</nav>
<main>{children}</main>
</>
)
}as per the last paragraph, both Client components and Server components are actually SSR (server side rendered). see Dan Abramov's explanation here - https://github.com/reactwg/server-components/discussions/4
2. depends. i've been using Context for most of my global Client state management. if you're more comfortable with Redux, why not. it's not an anti-pattern. there are truly things you'd need global client state management for like user authentication.
but for managing data that comes from your server, with Next 13 you can fetch data directly in your Server components using the fetch api, and you can use a tool like react-query for fetching inside your client components.
4 Replies
1. if you import a client component into a server component, it stays the same. your client component remains a client component while the other parts of your server component remain the same.
the idea for marking specific parts of your app as client and leaving the rest unmarked is so that you ship the least possible amount of client-side Javascript which helps to improve performance.
for example, in this layout file below, rather than make the whole layout a Client Component, move the interactive logic to a Client Component (e.g. <SearchBar />) and keep your layout as a Server Component. this means you don't have to send all the component Javascript of the layout to the client.
as per the last paragraph, both Client components and Server components are actually SSR (server side rendered). see Dan Abramov's explanation here - https://github.com/reactwg/server-components/discussions/4
2. depends. i've been using Context for most of my global Client state management. if you're more comfortable with Redux, why not. it's not an anti-pattern. there are truly things you'd need global client state management for like user authentication.
but for managing data that comes from your server, with Next 13 you can fetch data directly in your Server components using the fetch api, and you can use a tool like react-query for fetching inside your client components.
the idea for marking specific parts of your app as client and leaving the rest unmarked is so that you ship the least possible amount of client-side Javascript which helps to improve performance.
for example, in this layout file below, rather than make the whole layout a Client Component, move the interactive logic to a Client Component (e.g. <SearchBar />) and keep your layout as a Server Component. this means you don't have to send all the component Javascript of the layout to the client.
// SearchBar is a Client Component
import SearchBar from './searchbar'
// Logo is a Server Component
import Logo from './logo'
// Layout is a Server Component by default
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<>
<nav>
<Logo />
<SearchBar />
</nav>
<main>{children}</main>
</>
)
}as per the last paragraph, both Client components and Server components are actually SSR (server side rendered). see Dan Abramov's explanation here - https://github.com/reactwg/server-components/discussions/4
2. depends. i've been using Context for most of my global Client state management. if you're more comfortable with Redux, why not. it's not an anti-pattern. there are truly things you'd need global client state management for like user authentication.
but for managing data that comes from your server, with Next 13 you can fetch data directly in your Server components using the fetch api, and you can use a tool like react-query for fetching inside your client components.
Answer
helpful links - https://nextjs.org/docs/getting-started/react-essentials
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching
i also have a blog post where i explained when you may need to reach for a tool like Redux and when you're better off lifting state up, composing, etc
https://www.dayoawobeku.com/blog/simple-ways-to-manage-state-in-react-part-1
react query + context is also great - https://tkdodo.eu/blog/react-query-and-react-context
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching
i also have a blog post where i explained when you may need to reach for a tool like Redux and when you're better off lifting state up, composing, etc
https://www.dayoawobeku.com/blog/simple-ways-to-manage-state-in-react-part-1
react query + context is also great - https://tkdodo.eu/blog/react-query-and-react-context
@Dayo helpful links - https://nextjs.org/docs/getting-started/react-essentials
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching
i also have a blog post where i explained when you may need to reach for a tool like Redux and when you're better off lifting state up, composing, etc
https://www.dayoawobeku.com/blog/simple-ways-to-manage-state-in-react-part-1
react query + context is also great - https://tkdodo.eu/blog/react-query-and-react-context
Oak rough bulletgall waspOP
Thank you so much for that well explanation!!! - That was right on the spot, for what i needed to know!
And thanks for the links - Appreciate it a lot!
And thanks for the links - Appreciate it a lot!
awesome!