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

I just switched from ReactJS to NextJS and I can't find how to pass props from _app.js to a 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?

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?

Share Improve this question asked Mar 19, 2022 at 9:51 SJ19SJ19 2,12310 gold badges39 silver badges80 bronze badges 1
  • 1 How did you circumvent the issue since March? – Samuel Nihoul Commented May 25, 2022 at 12:15
Add a ment  | 

2 Answers 2

Reset to default 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
} 

In page.js

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

yourFunction(); // use the function
} 

Here is an example for redux

Cover <Component/> with redux <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>
    );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742366991a4430511.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信