ReactJS + NextJS - 如何将属性从 _app.js 传递到页面?

SJ1*_*J19 4 javascript reactjs next.js

我刚刚从 ReactJS 切换到 NextJS,但找不到如何将 props 从 _app.js 传递到页面。

我试图从不同的页面调用 _app.js 中的函数。在 ReactJS 中,这很简单,因为你必须创建自己的 Router,你可以将 props 从 App.js 传递到页面。现在在 NextJS 中,我没有显式调用该页面,因此我无法在任何地方传递 props。

在 NextJS 中如何做到这一点?

Ali*_*ain 6

_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
} 
Run Code Online (Sandbox Code Playgroud)

page.js

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

yourFunction(); // use the function
} 
Run Code Online (Sandbox Code Playgroud)