Reactjs 如何通过react上下文通过“contextType”而不是消费者注入“apollo client”?
我正在寻找Reactjs 如何通过react上下文通过“contextType”而不是消费者注入“apollo client”?,reactjs,apollo,react-apollo,Reactjs,Apollo,React Apollo,我正在寻找contextType向react组件注入apollo客户端的方法。我知道下面的代码可以工作,但它需要几行代码 import { ApolloConsumer } from "react-apollo"; const WithApolloClient = () => ( <ApolloConsumer> {client => "We have access to the client!" /* do stuff here */} </Ap
contextType
向react组件注入apollo客户端的方法。我知道下面的代码可以工作,但它需要几行代码
import { ApolloConsumer } from "react-apollo";
const WithApolloClient = () => (
<ApolloConsumer>
{client => "We have access to the client!" /* do stuff here */}
</ApolloConsumer>
);
从“react apollo”导入{ApolloConsumer};
const with apolloclient=()=>(
)介绍人提供了一种新的注入上下文的方法,而无需使用消费者
。它是由组件类的setcontextType
完成的。我想知道如何使用contextType
注入apollo客户端
?我应该使用什么contextType
呢?好的,我发现我可以从react apollo
导入ApolloContext
来解决它,如下所示:
import { ApolloContext } from 'react-apollo';
const HomeContainer = (props: HomeProps) => {
const context = useContext(ApolloContext);
return <Home {...props} />;
}
从'react apollo'导入{ApolloContext};
常量HomeContainer=(道具:HomeProps)=>{
const context=useContext(ApolloContext);
返回;
}
好的,我发现我可以从react apollo
导入ApolloContext
来解决它,如下所示:
import { ApolloContext } from 'react-apollo';
const HomeContainer = (props: HomeProps) => {
const context = useContext(ApolloContext);
return <Home {...props} />;
}
从'react apollo'导入{ApolloContext};
常量HomeContainer=(道具:HomeProps)=>{
const context=useContext(ApolloContext);
返回;
}