我已经用GraphQL安装了我的。一旦我启动了新的项目,并按照graphql的设置使用了react本机,我就得到了这个错误。
ApolloProvider没有被传递给客户端实例。确保你通过客户端通过你的客户。
我从上到下一直跟踪这个文档。
apollo.js
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
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;误差

"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",发布于 2019-10-31 06:52:48
在componentDidMount之前调用Render。因此,客户端实例在第一次呈现时将为空。这将导致错误。将呈现方法修改为
render() {
if(!this.state.client) {
return <Text>Loading</Text>
}
return (
<ApolloProvider client={this.state.client}>
<AppContainer />
</ApolloProvider>
);
}https://stackoverflow.com/questions/58637132
复制相似问题