-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
useEntityRecord
isn't refreshed when db updated externally
#47746
Comments
We can use the |
This will not work if using
|
@Tropicalista, can you share an example code for how you're fetching data with |
I try to explain better. This code works.
Using useEntityRecords does not work,
From my understanding both snippets do exact same thing. However I cannot invalidate cache if I'm using hook. |
If anyone else is stumbling upon this: I think the difference between the two is that |
What problem does this address?
useEntityRecord
only knows about updates to the database that it performs itself. If the db is updated by an custom REST API or AJAX endpoint, a cron job, etc, theuserRecord
doesn't automatically refresh. It only refreshes whenuserRecord.save()
is called.For example, the app below extends a 3rd party plugin. The 3rd party plugin uses custom REST API endpoints to update usermeta, and some of those values are added to the
/users
endpoint for convenience.https://github.com/WordPress/wporg-two-factor/blob/2640734b9295ce7f3891e4d1752ce4dd6d434469/settings/src/components/totp.js#L55-L71
https://github.com/WordPress/wporg-two-factor/blob/33b02cfa68492a0e658302f04dc8db58e1536954/settings/src/components/backup-codes.js#L40-L68
What is your proposed solution?
Add a
userRecord.refresh()
method that would fetch the latest copy of the resource and updateuserRecord.record
. This workaround demonstrates how I've done that without Core support, but a Core method could be cleaner.https://github.com/WordPress/wporg-two-factor/blob/00a7507/settings/src/utilities.js#L18-L34
cc @adamziel
The text was updated successfully, but these errors were encountered: