简体   繁体   English

Next.js 和 Typescript - 如何在功能组件中访问 app.js 的 props 和 staticProps

[英]Next.js with Typescript - how to access app.js props and staticProps in functional component

I want to know if it's possible for me to access props passed down from app.js to all Component Page using typescript and functional components.我想知道我是否可以使用 typescript 和功能组件访问从 app.js 传递到所有组件页面的道具。 Take a look at app.js and index.ts看看 app.js 和 index.ts

app.js:应用程序.js:

function MyApp({ Component, pageProps, router }) {

 const [message, setMessage] = useState(false)
 return (
   <Component setMessage={setMessage} key={router.route} {...pageProps} />
 )
}
export default MyApp

Index.ts:索引.ts:

function Home({products}): InferGetStaticPropsType<typeof getStaticProps>) {

 return (
   <div>Home</div>
 )
}

export function getStaticProps(){
   const products = []
}

I would like to be able to access the setMessage function AND the products from getStaticProps in the index.ts page.我希望能够在 index.ts 页面中访问 setMessage function 和 getStaticProps 中的产品。 I might be doing things the wrong way, thank you for your advice.我可能做错了,谢谢你的建议。

It is possible, just like with regular components.这是可能的,就像使用常规组件一样。 Pass your props along with pageProps , like you already do with your setMessage .将您的道具与pageProps一起传递,就像您已经使用setMessage一样。 Here is example .这是示例 I didn't followed your code precisely, but idea is the same.我没有完全按照你的代码,但想法是一样的。

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

相关问题 Next.js - 页面未将 GetServerSideProps 放入页面组件道具中(使用自定义 _app.js) - Next.js - Page not getting GetServerSideProps into Page component props (with custom _app.js) 等待_app.js加载,然后在Next.js上运行组件 - Wait _app.js loaded then run component on Next.js 如何在 Next.js 中获取 _app.js 的状态? - How to get state of _app.js in Next.js? 如何将 next.js 中的 pageProps 从子组件传递给 _app.js? - How to pass pageProps in next.js from a child component to _app.js? Next.JS typescript 通过 getServerSideProps 将 props 返回给外部组件 - Next.JS typescript pass getServerSideProps returned props to external component 在 React + Next.js + Typescript 中访问通过 URL 传递的 props - Access to props passed by URL in React + Next.js + Typescript 自定义 Next.js App 组件中未继承 React 道具 - React props are not inherited in Custom Next.js App component 为什么我无法访问 Next.js 应用程序“_app.js”中的 window? - Why can't I access the window in Next.js app '_app.js'? 在Next.JS 6中使用_app.js页面组件时,转换触发得太早了 - Transitions are firing too early when using _app.js page component in Next.JS 6 _app.js class 组件 Next.js 中的上下文 API 调度(消费者) - Context API dispatch (consumer) in _app.js class component Next.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM