简体   繁体   English

NextJS - 将客户端道具从 app.js 传递到特定组件

[英]NextJS - Passing client-side props from app.js to specific components

In traditional React, a common pattern is to define the Routers at the entry point, and pass whatever props you need to whichever component needs them, since they're all defined在传统的 React 中,一种常见的模式是在入口点定义路由器,并将所需的任何道具传递给需要它们的任何组件,因为它们都已定义

Eg,例如,

<Switch>
  <Route exact path="/">
    <Home prop1={prop1}/>
  </Route>
  <Route path="/about">
    <About prop1={prop1} prop2={prop2}/>
  </Route>
  <Route path="/dashboard">
    <Dashboard />
  </Route>
</Switch>

It's not clear to me how to do this in NextJS.我不清楚如何在 NextJS 中执行此操作。 The entry point _app.js has a generic that's used for all components.入口点 _app.js 有一个用于所有组件的泛型。 What would be the best way to pass prop1 to Home and About, but not Dashboard?将 prop1 传递给 Home 和 About 的最佳方式是什么,而不是 Dashboard?

To be clear, these are client-side props, not server-side or static props需要明确的是,这些是客户端道具,而不是服务器端或 static 道具

You can use getStaticProps , see the code below:您可以使用getStaticProps ,请参见下面的代码:

export async function getStaticProps(context) {
   return {
   props: {}, // will be passed to the page component as props
   // export this function from each page you want to pass props, in your 
   // case have this function on About, Home and Dashboard pages.
  }
}

for more check this link: getStaticProps有关更多信息,请查看此链接: getStaticProps

You can pass page specific props in getServerSideProps like below您可以在 getServerSideProps 中传递页面特定的道具,如下所示

import { GetServerSideProps } from "next";

const PageA = () => {
}

export const getServerSideProps: GetServerSideProps = async (ctx) => {
    return {
        props: {
            forbidden: true
        }
    }
}

export default PageA;

Then you can control that prop value in _app.js file and take action然后您可以在 _app.js 文件中控制该道具值并采取行动

const App = ({ Component, pageProps }) => {

  if (pageProps.forbidden) {
    return <Page403 />;
  }

  return (
    <Provider store={store}>
       <Component {...pageProps} />
    </Provider>
  )

}

So, think reversely.所以,反过来想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nextjs - 将 props 从自定义 _app.js 传递到<navbar>零件?</navbar> - Nextjs - passing props from custom _app.js to <Navbar> component? 从node.js(服务器端)获取数组以响应app.js(客户端) - Get array from node.js (server-side)to react app.js (client-side) ReactJS + NextJS - 如何将道具从 _app.js 传递到页面? - ReactJS + NextJS - How to pass props from _app.js to page? 我正在将一个道具从 App.js 传递给 MovieCard.js,但传递的道具显示为空 - I am passing a prop from App.js to MovieCard.js but props passed is showing empty 如何让子组件使用 App.js 道具数据中的 map()? - How to make child components use map() from App.js props data? 在 onclick 函数 next.js 中使用客户端道具 - using client-side props in onclick function next.js 在 App.js 文件中作为道具传递时,图像未在 DOM 中呈现? - Image is not rendering in the DOM while passing as props in the App.js file? Google-Maps-React:将道具传递给 App.js 的问题 - Google-Maps-React: Problems passing props to App.js 如何将值从app.js文件传递到节点js中的客户端文件? - How to pass value from app.js file to my client side file in node js? 将图像数据传递给客户端以获取JS图像URL参数 - Passing image data to client-side for JS image URL parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM