Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS + NextJS - How to pass props from _app.js to page?

I just switched from ReactJS to NextJS and I can't find how to pass props from _app.js to a page.

I'm trying to call a function in _app.js from a different page. In ReactJS it was straight forward, since you had to make your own Router, you could just pass props from App.js down to the pages. Now in NextJS I'm not explicitly calling the page, so I can't pass the props anywhere.

What is the way to do it in NextJS?

like image 304
SJ19 Avatar asked Oct 26 '25 07:10

SJ19


1 Answers

_app.js

function MyApp({ Component, pageProps }) {
const yourFunction(); // this is your function you created

return <Component {...pageProps} yourFunction ={yourFunction}/> // here you pass it as a prop
} 

In page.js

default export function Page({yourFunction}) { // destructure the function 

yourFunction(); // use the function
} 
like image 64
Ali AlHussain Avatar answered Oct 28 '25 21:10

Ali AlHussain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!