[英]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.我刚从 ReactJS 切换到 NextJS,我找不到如何将道具从 _app.js 传递到页面。
I'm trying to call a function in _app.js from a different page.我正在尝试从不同的页面调用 _app.js 中的函数。 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.
在 ReactJS 中这很简单,因为您必须制作自己的路由器,您可以将道具从 App.js 向下传递到页面。 Now in NextJS I'm not explicitly calling the page, so I can't pass the props anywhere.
现在在 NextJS 中我没有显式调用页面,所以我不能在任何地方传递道具。
What is the way to do it in NextJS?在 NextJS 中执行此操作的方法是什么?
_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
在
page.js
default export function Page({yourFunction}) { // destructure the function
yourFunction(); // use the function
}
Here is an example for redux这是一个redux的例子
Cover <Component/>
with redux <Provider/>
用 redux
<Provider/>
覆盖<Component/>
>
import { store } from "../redux/store/store";
import { Provider } from "react-redux";
export default function MyApp({ Component, pageProps }) {
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.