简体   繁体   English

apollo客户端如何在nextjs中使用SSR抓取数据?

[英]How to use SSR data fetching in nextjs with apollo client?

I am try to use apollo-client with nextjs.我正在尝试将apollo-client与 nextjs 一起使用。 Here I want to fetch data in getServerSideProps .在这里我想在getServerSideProps中获取数据。 Suppose I have 2 components and one page-假设我有 2 个组件和一页 -

section.tsx this is component-1 section.tsx这是组件 1

const Section = () => {
    return (
        <div>
            Section
        </div>
    );
};
export default Section;

mobile.tsx this is component 2 mobile.tsx这是组件 2

const Mobile = () => {
    return (
        <div>
            Mobile
        </div>
    );
};
export default Mobile;

Now I call this two component into home page.现在我将这两个组件调用到主页中。

index.tsx - index.tsx -

const Home: NextPage = () => {
  return (
    <Container disableGutters maxWidth="xxl">
      <Section />
      <Mobile />
    </Container>
  );
};
export default Home;

export const getServerSideProps: GetServerSideProps = async () => {
  const { data } = await client.query({ query: GET_USER_LIST })
  return { props: {} }
}

Here you can see that in getServerSideProps I already fetch my data.在这里你可以看到在getServerSideProps中我已经获取了我的数据。

My question is How can I directly access this data form Section component and Mobile component without passing props.我的问题是如何在不传递道具的情况下直接访问此数据表单Section组件和Mobile组件。 I don't want to pass props, because if my component tree will be more longer, then it will be difficult to manage props.我不想传递 props,因为如果我的组件树会更长,那么 props 将很难管理。

From appollo docs, I alreay know that apollo client do the same with redux state manager.appollo文档,我已经知道apollo clientredux state 经理做同样的事情。 So please tell me how can I access this data from any component that already fetched in getServerSideProps .所以请告诉我如何从已经在getServerSideProps中获取的任何组件访问这些数据。 Is it possible?.可能吗?。 If not then how can what is the solutions.如果不是那么怎么解决呢。

How about using context api if you want to avoid prop drilling?如果你想避免道具钻探,使用context api 怎么样? By putting data into context, you can access it from any child component.通过将数据放入上下文中,您可以从任何子组件访问它。 Get the data from the SSR and put it into the context.从 SSR 中获取数据并将其放入上下文中。

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

相关问题 Apollo Client 在 NextJS 中突变后不刷新数据? - Apollo Client is not refreshing data after mutation in NextJS? 如何从 NextJS 服务器对 Apollo 客户端进行水化 - How to hydrate Apollo client from NextJS server 将 Apollo Client 与 NextJS 一起使用时服务器端渲染数据? - Server-side render data when using Apollo Client with NextJS? NextJS SSR 是否适用于 Apollo 客户端? 在页面首次加载时检查“查看页面源代码”时,我看不到我的 HTML - Does NextJS SSR work with Apollo Client? When checking with 'view page source' on page first load I don't see my HTML 我如何使用带有本机 SSR(没有 NextJS)的反应钩子? - How i can use react hooks with native SSR (without NextJS)? 如何将 apollo-link-http 与 apollo-upload-client 一起使用? - How to use apollo-link-http with apollo-upload-client? Meteor/SSR/Apollo 客户端 - 尝试使用 Apollo 设置 SSR 并且无法全局找到获取 - Meteor/SSR/Apollo client - Trying to set up SSR with Apollo and getting fetch is not found gloablly NextJS 从组件中获取数据 - fetching data from component in NextJS 如何在 Apollo 中使用 readFragment 中的数据? - How to use data from readFragment in Apollo? Apollo Client 未在 Nextjs Lamda 中发送 JWT 不记名令牌 - Apollo Client not sending JWT bearer token in Nextjs Lamda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM