Skip to content

Commit

Permalink
Move the use of cloneDeep from lodash
Browse files Browse the repository at this point in the history
Instead, we will just spread the new meta value into a new object
  • Loading branch information
efuller committed Nov 29, 2023
1 parent f116504 commit d5e2378
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions packages/block-editor-tools/src/hooks/use-post-meta/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEntityProp } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { cloneDeep } from 'lodash';

/**
* A custom React hook that wraps useEntityProp for working with postmeta. This
Expand Down Expand Up @@ -31,16 +30,10 @@ const usePostMeta = (postType = null, postId = null) => {
: () => console.error(`Error attempting to set post meta for post type ${type}. Does it have support for custom-fields?`); // eslint-disable-line no-console

/**
* Define a wrapper for the setMeta function that performs a recursive clone
* of the meta object to ensure that there are no issues related to updating
* objects or array values within meta keys not triggering React or
* Gutenberg's state management system realizing that there is a change due
* to the fact that sub-items are stored as object references. These bugs are
* extremely difficult to find and correct, so it makes sense to include this
* functionality here as a catch-all on updates.
* Define a wrapper for the setMeta function that spreads the next meta value into a new object.
* @param {object} next - The new value for meta.
*/
const setMetaSafe = (next) => setMeta(cloneDeep(next));
const setMetaSafe = (next) => setMeta({ ...next });

return [meta, setMetaSafe];
};
Expand Down

0 comments on commit d5e2378

Please sign in to comment.