React-Apollo: Could not find quot;clientquot; in the context or passed in as an option(Reaction-Apollo:在上下文中找不到quot;客户端,或作为选项传入)
本文介绍了Reaction-Apollo:在上下文中找不到";客户端&,或作为选项传入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个使用Reaction和Apollo GraphQL的Web应用程序。
大多数时候我使用@apollo/react-hoc
,但一些遗留组件使用@apollo/react-components
。目前,我无法重构这些组件。
在我将路由移动到单独的文件后,我开始遇到此问题:
application-b17cb4b752e454bf66e1.js:303755 Uncaught Invariant Violation: Could not find "client" in the context or passed in as an option.
Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.
相关文件:
App.jsx
:
import {ApolloProvider} from '@apollo/react-hoc';
import ApolloClient from 'apollo-boost';
import {InMemoryCache, IntrospectionFragmentMatcher} from 'apollo-cache-inmemory';
import React from 'react';
import {BrowserRouter} from 'react-router-dom';
import introspection from '../../graphql/introspection.json';
import Routes from './routing/Routes';
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData: introspection.data
});
const cache = new InMemoryCache({fragmentMatcher});
const client = new ApolloClient({
cache,
fetchOptions: {
credentials: 'same-origin'
}
});
class App extends React.Component {
render() {
return (
<ApolloProvider client={client}>
<BrowserRouter>
<Routes/>
</BrowserRouter>
</ApolloProvider>
);
}
}
Login.jsx
(此处Mutation
组件导致该错误):
import {Mutation} from '@apollo/react-components';
import {gql} from 'apollo-boost';
import {Formik} from 'formik';
import React from 'react';
import {Redirect, withRouter} from 'react-router-dom';
import '../../styles/main';
const LOGIN = gql`mutation Login($email:String!, $password:String!){
login(email: $email, password: $password){token user{email id name}}
}`;
class Login extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
if (this.state.redirectToReferrer) {
const {from} = this.props.location.state || {from: {pathname: '/'}};
return <Redirect to={from}/>;
}
return (
<div className={'ui-login-background'}>
<div className={'ui-login-form-container'}>
<Mutation
mutation={LOGIN}
update={(cache, response) => {
cache.writeQuery({
query: gql`{me{id}}`,
data: {me: response.data.login.user}
});
}}>
{(login) => (
<Formik
initialValues={{email: '', password: ''}}
onSubmit={(values, {setSubmitting}) => {
login({variables: values})
.then(({data}) => {
if (data && data.login && data.login.token) {
this.setState({
redirectToReferrer: true
});
} else {
this.setState({loginFailed: true});
}
});
setSubmitting(false);
}}
render={
() => ...markup...
}/>
)}
</Mutation>
</div>
</div>
);
}
}
export default withRouter(Login);
包依赖项:
{
"dependencies": {
"@apollo/react-components": "^3.1.3",
"@apollo/react-hoc": "^3.1.2",
"@jbuschke/formik-antd": "^1.2.1",
"apollo-boost": "^0.4.3",
"apollo-cache-inmemory": "^1.6.3",
"graphql": "^14.4.2",
"graphql-tag": "latest",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1"
// ...and more...
}
}
Login.jsx
是一个遗留组件,不幸的是我无法更改它。
此错误的原因是什么?如何修复?
推荐答案
问题在@apollo/react-hoc
和@apollo/react-components
版本中。如package.json
所示,components
有版本3.1.3
,hoc
有3.1.2
。
这篇关于Reaction-Apollo:在上下文中找不到";客户端&,或作为选项传入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Reaction-Apollo:在上下文中找不到";客户端&,或作为选项传入
基础教程推荐
猜你喜欢
- 在for循环中使用setTimeout 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 动态更新多个选择框 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01