首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ApolloProvider没有被传递给客户端实例

ApolloProvider没有被传递给客户端实例
EN

Stack Overflow用户
提问于 2019-10-31 04:59:05
回答 1查看 4K关注 0票数 4

我已经用GraphQL安装了我的。一旦我启动了新的项目,并按照graphql的设置使用了react本机,我就得到了这个错误。

ApolloProvider没有被传递给客户端实例。确保你通过客户端通过你的客户。

我从上到下一直跟踪这个文档

apollo.js

代码语言:javascript
复制
import {HttpLink} from 'apollo-link-http';
import {ApolloClient} from 'apollo-client';
import {InMemoryCache} from 'apollo-cache-inmemory';

const makeApolloClient = token => {
  // create an apollo link instance, a network interface for apollo client
  const link = new HttpLink({
    uri: `https://learn.hasura.io/graphql`,
    headers: {
      Authorization: `Bearer ${token}`,
    },
  });
  // create an inmemory cache instance for caching graphql data
  const cache = new InMemoryCache();
  // instantiate apollo client with apollo link instance and cache instance
  const client = new ApolloClient({
    link,
    cache,
  });
  return client;
};
export default makeApolloClient;

App.js

代码语言:javascript
复制
import React from 'react';
import AppContainer from '../routes/Routes';
import AsyncStorage from '@react-native-community/async-storage';
import {ApolloProvider} from 'react-apollo';
import makeApolloClient from '../apollo/apollo';

console.disableYellowBox = true;

class App extends React.Component {
  state = {
    client: null,
  };

  async componentDidMount() {
    // fetch session
    const session = await AsyncStorage.getItem('@todo-graphql:session');
    const sessionObj = JSON.parse(session);
    const {token, id} = sessionObj;
    // make apollo client with this session token
    const client = makeApolloClient(token);
    // start emitting events saying that the useri s online
    this.setState({client});
  }

  render() {
    return (
      <ApolloProvider client={this.state.client}>
        <AppContainer />
      </ApolloProvider>
    );
  }
}

export default App;

误差

代码语言:javascript
复制
"apollo-cache-inmemory": "^1.6.3",
"apollo-client": "^2.6.4",
"apollo-link-http": "^1.5.16",
"react": "16.9.0",
"react-apollo": "^3.1.3",
"react-native": "0.61.3",
EN

回答 1

Stack Overflow用户

发布于 2019-10-31 06:52:48

在componentDidMount之前调用Render。因此,客户端实例在第一次呈现时将为空。这将导致错误。将呈现方法修改为

代码语言:javascript
复制
render() {
  if(!this.state.client) {
    return <Text>Loading</Text>
  }
  return (
      <ApolloProvider client={this.state.client}>
        <AppContainer />
      </ApolloProvider>
    );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58637132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档