Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export useQueryClient from @contentful/field-editor-reference #1808

Open
M1r1k opened this issue Dec 3, 2024 · 2 comments · May be fixed by #1809
Open

Export useQueryClient from @contentful/field-editor-reference #1808

M1r1k opened this issue Dec 3, 2024 · 2 comments · May be fixed by #1809

Comments

@M1r1k
Copy link

M1r1k commented Dec 3, 2024

It would be great to have access to client instance itself on top of SharedQueryClientProvider to be able to invalidate or set data to cache storage from the app code, because right now it is pretty hard to update entry title in case it was modified externally.

M1r1k added a commit to M1r1k/field-editors that referenced this issue Dec 3, 2024
export react-query client instance together with
SharedQueryClientProvider to allow cache invalidation, cache setting, as
well as reusing existing cache for entities fetching

✅ Closes: contentful#1808
@ManasiRoy
Copy link

import React, { useEffect, useState } from 'react';
import { useQueryClient } from '@contentful/field-editor-reference';

const EntryTitleUpdater = ({ entryId, newTitle }) => {
const queryClient = useQueryClient();
const [isUpdating, setIsUpdating] = useState(false);

useEffect(() => {
const updateEntryTitleInCache = () => {
setIsUpdating(true);

  queryClient.setQueryData(['entry', entryId], (prevData) => {
    if (!prevData) return prevData;
    
    return { 
      ...prevData,
      title: newTitle
    };
  });
  
  queryClient.invalidateQueries('entries');
  
  setIsUpdating(false);
};

// If newTitle is available, update the title
if (newTitle) {
  updateEntryTitleInCache();
}

}, [newTitle, entryId, queryClient]);

return (


Entry Title Updater


{isUpdating ? 'Updating title in cache...' : 'Title updated successfully!'}



);
};

export default EntryTitleUpdater;

You can please try this. Thanks. Happy Coding :)

@M1r1k
Copy link
Author

M1r1k commented Dec 3, 2024

Hi @ManasiRoy that was the first thing I have tried, but unfortunately is not exported from this package. Only provider is, you can find it here https://github.com/contentful/field-editors/blob/master/packages/reference/src/index.tsx#L26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants