简体   繁体   English

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

[英]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.jspage.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.

相关问题 Nextjs - 将 props 从自定义 _app.js 传递到<navbar>零件?</navbar> - Nextjs - passing props from custom _app.js to <Navbar> component? 将数据(prop)从 _app.js 传递到页面中的 getServerSideProps - NextJS,最新版本 - Pass data (prop) from _app.js to getServerSideProps in a page - NextJS, latest version 在 NextJS 中如何将数据从 server.js 传递到 _app.js? - In NextJS how to pass data from server.js to _app.js? React Native:如何将 props 变量从其他 js 文件传递到 app.js 文件 - React Native: How to pass a props variable from other js file to app.js file 如何将道具从 App.js 传递到我的登录组件? - How can I pass in props from App.js to my login component? 如何在 ReactJS v16+ 中将获取的 object 从 App.js 异步传递给子组件 - How to pass a fetched object from App.js to a child component asynchronously in ReactJS v16+ NextJS - 将客户端道具从 app.js 传递到特定组件 - NextJS - Passing client-side props from app.js to specific components 如何从App.js更新呈现的React组件的道具? - How to update the props of a rendered react component from App.js? 如何在 NextJS、Reactjs 中将道具传递给 {props.children} - How to pass props to {props.children} in NextJS, Reactjs React/Nextjs - 知道在 _app.js 上渲染了什么页面 - React/Nextjs - know what page is rendered on _app.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM