Unhandled Runtime ErrorError: Maximum call stack size exceeded
Unanswered
Horned oak gall posted this in #help-forum
Horned oak gallOP
In my nextjs app/page.tsx is using async fetching data, and I want to render data in a component, but within the component I would like to use "useSession" to filter data condition, but "useSession" only working in client side, When I add "use client" in the component however there is an error,
" Maximum call stack size exceeded,React from 'react'"
" Maximum call stack size exceeded,React from 'react'"
//app/page.tsx
import Image from 'next/image'
import Searchbar from '@/components/Searchbar'
import HeroCarousel from '@/components/HeroCarousel'
import { getAll } from '@/lib/actions'
import UserSession from '@/components/UserSession'
import DisplayCard from '@/components/DisplayCard'
const Home = async () => {
const allProducts = await getAll()
return (
<>
<section className="trending-section">
<h2 className="section-text">We You Tracked</h2>
<div className="flex flex-wrap justify-center gap-x-8 gap-y-16">
{/* <DisplayMiddleWare productList={allProducts}/> */}
{
allProducts?.map((product)=>(
<DisplayCard key={product._id} product={product}/>
))
}
</div>
</section>
<UserSession/>
</>
)
}
export default Home//DisplayCard.tsx
"use client"
import { Product } from '@/types'
import Link from 'next/link'
import Image from 'next/image'
import React from 'react';
import { useSession } from 'next-auth/react';
interface Props {
product: Product;
}
const DisplayCard = ({ product}: Props) => {
const { status, data: session } = useSession();
const isInList = session?.user?.search?.includes(product._id)
return isInList ? (
<Link href={`/products/${product._id}`} className="product-card">
<div className='product-card_img-container border rounded-md overflow-hidden'>
//the login to display data
</div>
</Link>
) : ''
}
export default DisplayCard118 Replies
In my nextjs app/page.tsx is using async fetching data, and I want to render data in a component, but within the component I would like to use "useSession" to filter data condition, but "useSession" only working in client sideTherefore you should create a client component on a different file, and delegate your client logic there.
Horned oak gallOP
those are my latest code but still same bug, app/page.tsx
import Image from "next/image";
import Searchbar from "@/components/Searchbar";
import UserSession from "@/components/UserSession";
import HeroCarousel from "@/components/HeroCarousel";
import DisplayCardClient from "@/components/DisplayCardClient";
import { getAll } from '@/lib/actions';
const Home = async () =>{
const allProducts = await getAll();
return (
<>
<section className="px-6 md:px-20 py-24">
<div className="flex max-xl:flex-col gap-16">
<div className="flex flex-col justify-center">
<p className="small-text">
Always Make sure Lowest Price:
<Image
src="/assets/icons/arrow-right.svg"
alt="arrow-right"
width={16}
height={16}
/>
</p>
<h1 className="head-text">
Unleash the Power of
<span className="text-primary"> PriceTracker</span>
</h1>
<p className="mt-6">
Powerful, self-serve product and growth analytics to help you convert, engage, and retain more.
</p>
<Searchbar />
</div>
<HeroCarousel />
</div>
</section>
<section className="trending-section">
<h2 className="section-text">What You Tracked</h2>
<div className="flex flex-wrap justify-center gap-x-8 gap-y-16">
{allProducts?.map((product) => (
<DisplayCardClient key={product._id} product={product} />
))}
</div>
</section>
<UserSession />
</>
);
};
export default Homewhats latest the error?
Horned oak gallOP
Unhandled Runtime Error
Error: Maximum call stack size exceeded
Call Stack
String.replace
<anonymous>
Error: Maximum call stack size exceeded
Call Stack
String.replace
<anonymous>
like this
Horned oak gallOP
I was stucked in the stack size error for 2 days 😢
"use server"
import { dbConn } from "../mongoose"; export async function getAll(){
try{
dbConn()
const products = await Product.find()
return products
}catch(err){
console.log(err)
}
}this is how I write the getAll()
Only if I passed in data receiving value from getAll() to "use client" component, then the error occur
@Horned oak gall Only if I passed in data receiving value from getAll() to "use client" component, then the error occur
couldve told me this in the beginning.
Horned oak gallOP
sorry
I have just tested the result
so, what do you think the error from ?
try console logging
allProductsHorned oak gallOP
originally printed some
then
hmmmm
try this
{allProducts?.map((product) => (
<div>test</div>
))}Horned oak gallOP
yeah, working
If I pass value but the component not using "use client" then no this error
Horned oak gallOP
"use client"
import { Product } from '@/types'
import Link from 'next/link'
import Image from 'next/image'
interface Props {
product : Product
}
const DisplayCard = ({product}:Props) => {
return (
<Link href={`/products/${product._id}`} className="product-card">
<div className='product-card_img-container border rounded-md overflow-hidden'>
<Image
src={product.image}
alt={product.title}
width={200}
height={200}
className='product-card_img'
/>
</div>
<div className='flex flex-col gap-3'>
<h3 className='product-title'>{product.title}</h3>
<div className='flex justify-between'>
<p className='text-black opacity-50 text-lg capitalize'>
{product.category}
</p>
<p className='text-black text-lg font-semibold'>
<span>{product?.currency}</span>
<span>{product?.currentPrice}</span>
</p>
</div>
</div>
</Link>
)
}
export default DisplayCardtsxcan you use codeblock?
Horned oak gallOP
This is the DisplayCard, it is working if I don't use "use client" at top
```tsx
paste code here
```
paste code here
```
Horned oak gallOP
ok
Horned oak gallOP
damn
I am stupid
ok
Horned oak gallOP
nice
@Horned oak gall then
this is might be the problem
can you try console.logging
allProducts.lengthHorned oak gallOP
ts const allProducts = await getAll();
console.log("From app/next.tsx", allProducts?.length)From app/next.tsx 2
Warning: Each child in a list should have a unique "key" prop.
Check the top-level render call using <div>
From app/next.tsx 2
2 only?
what the hecc ðŸ˜
Horned oak gallOP
So, that was really weird bug
😢
can you paste the whole log of console.log
allProductsoh wait
can you try
console.log( JSON.stringify(allProducts, null, 2) )Horned oak gallOP
like this
so I was trying to fix the bug by using getStaticProps to remove async keyword in function, but the getStaticProps wasn't support in app/.
GG
bruh
so what caused the issue?
Horned oak gallOP
not know
i still believe its part of your getData function not parsing correctly
@Horned oak gall `"use server"
import { dbConn } from "../mongoose"; export async function getAll(){
try{
dbConn()
const products = await Product.find()
return products
}catch(err){
console.log(err)
}
}`
try returning only a part of the data and not the whole data
Horned oak gallOP
nuh。。。
same
damn, maybe tmr will be fixed
ah
Horned oak gallOP
I will come back tmr morning
too sleepy
{allProducts?.map((product, i) => (
<DisplayCardClient key={i}/>
))}try this then
Horned oak gallOP
ok
this is the DisplayCardClient;
ofc u need to remove the props too
Horned oak gallOP
emmmmmmm
since I don't pass data to DisplayCard from DisplayCardClient, show the DisplayClient component not showing
but error was removed
whats the difference betweenD isplayCard and DisplayCardClient?
in here you wrote DisplayCardClient? why is there DisplayCard?
@Horned oak gall In my nextjs app/page.tsx is using async fetching data, and I want to render data in a component, but within the component I would like to use "useSession" to filter data condition, but "useSession" only working in client side, When I add "use client" in the component however there is an error,
" Maximum call stack size exceeded,React from 'react'"
tsx
//app/page.tsx
import Image from 'next/image'
import Searchbar from '@/components/Searchbar'
import HeroCarousel from '@/components/HeroCarousel'
import { getAll } from '@/lib/actions'
import UserSession from '@/components/UserSession'
import DisplayCard from '@/components/DisplayCard'
const Home = async () => {
const allProducts = await getAll()
return (
<>
<section className="trending-section">
<h2 className="section-text">We You Tracked</h2>
<div className="flex flex-wrap justify-center gap-x-8 gap-y-16">
{/* <DisplayMiddleWare productList={allProducts}/> */}
{
allProducts?.map((product)=>(
<DisplayCard key={product._id} product={product}/>
))
}
</div>
</section>
<UserSession/>
</>
)
}
export default Home
tsx
//DisplayCard.tsx
"use client"
import { Product } from '@/types'
import Link from 'next/link'
import Image from 'next/image'
import React from 'react';
import { useSession } from 'next-auth/react';
interface Props {
product: Product;
}
const DisplayCard = ({ product}: Props) => {
const { status, data: session } = useSession();
const isInList = session?.user?.search?.includes(product._id)
return isInList ? (
<Link href={`/products/${product._id}`} className="product-card">
<div className='product-card_img-container border rounded-md overflow-hidden'>
//the login to display data
</div>
</Link>
) : ''
}
export default DisplayCard
Horned oak gallOP
Here was my orinial code which only has app/page.tsx and DisplayCard.tsx, as your suggestion "Therefore you should create a client component on a different file, and delegate your client logic there." I create a new Component called DisplayCardClient.tsx was generate by chatgpt
import { Product } from '@/types'
import Link from 'next/link'
import Image from 'next/image'
interface Props {
product : Product
}
const DisplayCard = ({product}:Props) => {
return (
<Link href={`/products/${product._id}`} className="product-card">
<div className='product-card_img-container border rounded-md overflow-hidden'>
<Image
src={product.image}
alt={product.title}
width={200}
height={200}
className='product-card_img'
/>
</div>
<div className='flex flex-col gap-3'>
<h3 className='product-title'>{product.title}</h3>
<div className='flex justify-between'>
<p className='text-black opacity-50 text-lg capitalize'>
{product.category}
</p>
<p className='text-black text-lg font-semibold'>
<span>{product?.currency}</span>
<span>{product?.currentPrice}</span>
</p>
</div>
</div>
</Link>
)
}
export default DisplayCardThe original DisplayCard.tsx is completed working when it looked like this
only if I add "use client" at the top the error Maximum call stack size exceeded will occur
Horned oak gallOP
@aardani hello bro,

where is the DisplayCardClient Code?
Horned oak gallOP
oh I have just skip the component, cause it is not used any more
have you ever meet this kind of error before?
its a common beginner's mistake
Horned oak gallOP
just because use client
I mean the error is apparent
Horned oak gallOP
how should I debug
something went wrong from passing data from server to client
then when you console.log the server data
you get Maximum call stack exceeded
maybe it has something to dow with the size of the data
Horned oak gallOP
If I remove "use client" in DisplayCard.tsx then I have no error when console log server side data
yes, you said that like 4 times already
removing "use client" is not the solution.
If you want to use
If you want to use
useSession you need a client componentHorned oak gallOP
yes
yes
Horned oak gallOP
emmmmm, so I don't konw the solution
try passing just one product instead of multiple ones
Horned oak gallOP
I will try
<DisplayCardClient product={allProducts[0]} />then 1
Horned oak gallOP
I test from 0 - 6 since the lenght was 7, the result was all passed
Horned oak gallOP
but when I uncomment "use client" then even just one index will still be error
I have even just try only leave an empty div, still the same error
<div></div>Horned oak gallOP
I try later put "use client" at top of Home and use useEffect
the error come only when I console log the product
Horned oak gallOP
@aardani help
i have no idea
seems like theres something wrong int he project
try to reproduce the bug on a minimal proejct :/