我刚从ReactJS切换到NextJS,我找不到如何将道具从_app.js传递到页面。
我正在尝试从另一个页面调用_app.js中的函数。在ReactJS中,它是直接的,因为您必须创建自己的路由器,所以您可以直接将道具从App.js传递到页面。现在,在NextJS中,我没有显式地调用页面,所以我不能将道具传递到任何地方。
在NextJS中有什么方法可以做到呢?
发布于 2022-03-19 13:33:58
_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
} 在page.js中
default export function Page({yourFunction}) { // destructure the function
yourFunction(); // use the function
} 发布于 2022-07-18 19:24:35
下面是redux的一个示例
用redux <Component/>覆盖<Provider/>
import { store } from "../redux/store/store";
import { Provider } from "react-redux";
export default function MyApp({ Component, pageProps }) {
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
);
}https://stackoverflow.com/questions/71537121
复制相似问题