Next.js Discord

Discord Forum

Next.js 13 app directory static site generation and show without props drilling

Unanswered
Saltwater Crocodile posted this in #help-forum
Open in Discord
Saltwater CrocodileOP
,,,
product/page.tsx

import CompA from "./CompA";
const apicall = async () => {
    const res = await fetch("https://fakestoreapi.com/products", {
        next:{
            revalidate: 10 // 10 second 
        }
    });
    return await res.json()
}
const page = async () => {
    const data = await apicall()
  return (
    <>
    <CompA product={data} />
    </>
  )
}
export default page


Here this is my first product page and I am receiving some data from API. I want to show the data in a nested component called <CompB/> But On my product page initially I am calling <CompA/> and I am passing the data in into <CompA/> which I was given from API.
And this is component A.


'use client'
import React from 'react'
import { Product } from './product.types'
import CompB from './CompB'
const CompA = ({product}:{product:Product[]}) => {
    
  return (
    <>
<CompB product={product} />
    </>
  )
}
export default CompA


And <CompA/> doesn't want to show the data instead pass the data into <Comb/>
'use client'
import React from 'react'
import Image from 'next/image';
import { Product } from './product.types';
import { FaStar } from "react-icons/fa";
const Compb = ({ product }: { product: Product[] }) => {
  return (
    <>
      // show the data here
       
    </>
  );
};

export default Compb

So here the problem is Unnessesare props drilling. is there any way to avoid the props drilling and show the data directly, in <Comb/>

Actually I want to use RTK in my project or any other state management library

37 Replies

Saltwater CrocodileOP
Sorry Guys for the spelling mistake. Now i can't edit the post
Do you really need both CompA and CompB to be client components? If not, you can make them server components and fetch that data directly where you need it avoiding prop drilling
@not-milo.tsx Do you really need both CompA and CompB to be client components? If not, you can make them server components and fetch that data directly where you need it avoiding prop drilling
Saltwater CrocodileOP
Yes, I need Both components a and b in a big project. But I can't fetch directly from the component level because if fetch data from component b then <Comb/> will no longer have client components
Then use context
Burmese
If compA can be a server component, you can fetch from it and pass directly to compB
You can wrap both components in context and then access the data directly from CompB with a custom hook
@Burmese If compA can be a server component, you can fetch from it and pass directly to compB
Saltwater CrocodileOP
But in that way actually, I don't want. I need redux flavor
@not-milo.tsx You can wrap both components in context and then access the data directly from CompB with a custom hook
Saltwater CrocodileOP
Can I use context in server side??
If you really know what you're doing yes, but it's not worth it.
Burmese
I find it peculiar that you have such a strong preference for redux given such a contrived example. I would recommend taking a step back and trying to think how to structure your components from a unilateral data flow.
If you have a small group of client components that need access to server fetched data, use context. If you have a bunch of components that are deeply nested or a CompH that is somewhere else, then a global state option may be beneficial. But still I would explore fetching data from a server component wherever the data is needed first.
@Burmese I find it peculiar that you have such a strong preference for redux given such a contrived example. I would recommend taking a step back and trying to think how to structure your components from a unilateral data flow.
Saltwater CrocodileOP
We use react because we split everything component-wise. React is a component-based library. When I need server-side rendering and need data into a deeply nested component it becomes really complex. Props drilling is really hell
I need the data directly from 1 to 6 as the image shows. avoiding props drilling.
is there any way to do this??
Either make 6 a server component and fetch data there or use react's context api
Burmese
If 4 is a server component, fetch the data and pass it as props to 6. If 1-7 are client components, imagine a square around this tree. That's context.
@not-milo.tsx Either make `6` a server component and fetch data there or use react's context api
Saltwater CrocodileOP
Only Component 1 will server Component and other components will be client components because I need to use useState() hook in every component. thats I need
Then use react's context api
Burmese
Then depending on 2 and 4, decide which node to wrap context
Saltwater CrocodileOP
Good suggestions. Is contextApi worth it??
Definitely. It's way more maintainable than prop drilling
Saltwater CrocodileOP
This is the error i cant use createContext in server component
It's not that simple. You need a context provider which will be a client component. The you can import that inside a server component and wrap it around whatever portion of your component tree needs it.
Saltwater CrocodileOP
I already did this but again error has been given
Saltwater CrocodileOP
This is not well documented 😓 . I can't understand Tnx for your reply
from where i can get thrid party provider??
Third-party context providers are those developed by other people. For example, let's say you're using Clerk for user authentication. Clerk will give you an auth provider that you can use to wrap your application, and that's a third-party context provider.
If you need a complete guide on how to use the context api with Next.js 13 and the app router here's a good one: https://codevoweb.com/setup-react-context-api-in-nextjs-13-app-directory/
I'm sorry, what is?
Saltwater CrocodileOP
This is client-side rendering using context api . I want server-side rendering(SSR)
or static site generation(SSG) and show data inside nested component without props drilling
@not-milo.tsx I'm sorry, what is?
Saltwater CrocodileOP
@not-milo.tsx With respect brother do you know about server-side rendering or static site generation in Next.js???
With respect, I do. I'm just having a hard time understanding what you need since your messages are quite hard to read and I don't know the extent of your knowledge of React, RSCs and Next.js...
@Saltwater Crocodile This is client-side rendering using context api . I want server-side rendering(SSR) or static site generation(SSG) and show data inside nested component without props drilling
Using context doesn't make your application client side all of a sudden. It just adds bits of interactivity to your UI. If you're having trouble understanding the relationship between server components and client components I suggest you read as much as you can from this documentation section: https://nextjs.org/docs/app/building-your-application/rendering
Saltwater CrocodileOP
okay. Have you ever faced a problem with Next.js server-side rendering and props drilling? how did you overcome from that problems??
Do you have any open-source project code with next.js server-side rendering?
I would like to contribute and I want to see how you have overcome that.