Reactjs 观察缓存是否已更新,但直接使用查询的组件不会重新渲染
我有一个根组件,它包含包含数据的表组件和使用usehook的编辑器组件。当我使用UseMartation创建一个新的数据时,我的表不会重新呈现,即使我更新了缓存,甚至我让客户知道了这个变化。在使用UseHook创建新数据后,如何让表组件知道并触发重新渲染 这是我的客户:Reactjs 观察缓存是否已更新,但直接使用查询的组件不会重新渲染,reactjs,graphql,apollo,react-apollo,apollo-client,Reactjs,Graphql,Apollo,React Apollo,Apollo Client,我有一个根组件,它包含包含数据的表组件和使用usehook的编辑器组件。当我使用UseMartation创建一个新的数据时,我的表不会重新呈现,即使我更新了缓存,甚至我让客户知道了这个变化。在使用UseHook创建新数据后,如何让表组件知道并触发重新渲染 这是我的客户: /* eslint-disable import/no-extraneous-dependencies */ import { ApolloClient } from 'apollo-client'; import { In
/* eslint-disable import/no-extraneous-dependencies */
import { ApolloClient } from 'apollo-client';
import {
InMemoryCache,
IntrospectionFragmentMatcher,
} from 'apollo-cache-inmemory';
import { createUploadLink } from 'apollo-upload-client';
import introspectionQueryResultData from './fragmentTypes.json';
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData,
});
const cache = new InMemoryCache({ fragmentMatcher });
const client = new ApolloClient({
link: createUploadLink({ uri: 'https://rtpbackend.ezileli.dev/graphql' }),
cache,
});
export default client;
以下是我的建议:
const [createFunc] = useMutation(CREATE_PARTNER, {
update(cache, { data }) {
const { partners } = cache.readQuery({
query: PARTNERS,
variables: {
condition: {
limit: 10,
offset: 0,
order: [['createdAt', 'desc']],
},
},
});
const operationName = CREATE_PARTNER.definitions[0].name.value;
const response = data[operationName].partner;
client.writeQuery({
query: PARTNERS,
data: {
partners: {
...partners,
totalCount: partners.totalCount + 1,
data: partners.data.concat([response]),
},
},
});
},
});