@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
36 lines (35 loc) • 1.15 kB
JavaScript
// packages/core-data/src/hooks/use-entity-prop.js
import { useCallback } from "@wordpress/element";
import { useDispatch, useSelect } from "@wordpress/data";
import { STORE_NAME } from "../name";
import useEntityId from "./use-entity-id";
function useEntityProp(kind, name, prop, _id) {
const providerId = useEntityId(kind, name);
const id = _id ?? providerId;
const { value, fullValue } = useSelect(
(select) => {
const { getEntityRecord, getEditedEntityRecord } = select(STORE_NAME);
const record = getEntityRecord(kind, name, id);
const editedRecord = getEditedEntityRecord(kind, name, id);
return record && editedRecord ? {
value: editedRecord[prop],
fullValue: record[prop]
} : {};
},
[kind, name, id, prop]
);
const { editEntityRecord } = useDispatch(STORE_NAME);
const setValue = useCallback(
(newValue) => {
editEntityRecord(kind, name, id, {
[prop]: newValue
});
},
[editEntityRecord, kind, name, id, prop]
);
return [value, setValue, fullValue];
}
export {
useEntityProp as default
};
//# sourceMappingURL=use-entity-prop.js.map