Just for reference, I am creating a shopping cart for an e-commerce website following an old tutorial and updating it to Next.js 13+ as I code.
In the previous version of the tutorial, the _app.ts
file was using page routing. The code snippet looked like this:
return (
<>
<Navbar cart={cart} addToCart={addToCart} removeFromCart={removeFromCart} />
<Component cart={cart} addToCart={addToCart} removeFromCart={removeFromCart} {...pageProps} />
</>
);
Now, with the new App Routing in Next.js 13+, I have the following code in the layout.ts
file using app routing:
return (
<html lang="en">
<body className={inter.className}>
<Navbar cart={cart} addToCart={addToCart} removeFromCart={removeFromCart} />
{children} {/* <-- Alternative code to pass props? */}
</body>
</html>
);
Are there any other ways to pass props in this new setup, apart from using Redux. Any guidance or suggestions would be greatly appreciated.
2
Answers
useContext did the work for me. Redux seemed overkill and there were no other ways to pass props (as per the replies). I wanted to update the states globally so fetching data doesn't work.
According to the App Router documents:
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
This is an example of how they want you to fetch data in the
page.tsx
file:Data fetching is mainly based around using
fetch()
. But you can see a lot more examples of what is doable here:https://nextjs.org/docs/app/building-your-application/data-fetching/fetching