reactjs - ApolloConsumer VS 导入客户端
问题描述
为什么我应该使用 ApolloConsumer 而不是直接在我的模块中导入客户端?
从文档中我应该做类似的事情:
// Module A.js initiate client
const client = new ApolloClient({
// init cache, links, typeDefs...
});
export default client;
// Module index.jsx
import client from 'A';
ReactDOM.render(
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
document.getElementById('root'));
// Any other component not using Query or Mutation
const Other = () => (
<ApolloConsumer>
{
client => {
// use client
}
}
</ApolloConsumer>);
但是为什么不直接导入没有 ApolloConsumer 的客户端呢?
// Module OtherBis
import client from 'A';
const AltOther () => {
// do something with client
return <div></div>;
};
解决方案
在我看来,ApolloConsumer
组件是为支持而创建的,JSX
就像react-relay
for一样relay
。没有必要。其实我从来没用过ApolloConsumer
。此外,还有可以做任何事情的钩子(例如useQuery
,、、useMutation
) 。useSubscription
他们只是做一个工具。是否使用它取决于您。
推荐阅读
- python - 使用 OpenCV 对 pytesseract OCR 进行图像预处理
- python - 子集根据他们的标签
- python - 如何使用 Django 和 Pandas 将 excel 文件返回给用户
- asp.net - ASP.NET 根据 URL 改变布局
- pandas - 熊猫四舍五入
- angular - 角度 10 翻译字符串 ID 不匹配
- c++ - 带有扩展类的 c++ 读取访问冲突
- r - 在R表中排序和打印最大值和关联的行名
- android - 使用 Theme.MaterialComponents.Light.DarkActionBar 后缺少菜单栏
- python-3.x - 是否有将字符串拆分为单个字符的方法?(Python)