@excentone/spfx-react
Version:
Contains custom ReactJs components and hooks intended to use when developing SharePoint Framework (SPFx) Web components.
26 lines (24 loc) • 1.28 kB
JavaScript
import { useMemo } from "react";
import { values } from "@fluentui/react";
import { get } from "@microsoft/sp-lodash-subset";
import { useComponentContext } from "../contexts";
/**
* `usePropertyValue` is used to get the value of the property from the SPFx webpart's or application extension's `properties` object, which usually contains the configuration for the whole webpart/application extension.
* @param propertyPath The path to the property to get the value of.
* @param defaultValue The value to return if the property path was not found. Default: `undefined`.
* @returns The value of the property, or undefined if the property path does not exists in the `properties` object.
*/
export const usePropertyValue = (propertyPath, defaultValue = undefined, converterFunction) => {
const { properties } = useComponentContext();
const propertyValue = useMemo(() => {
if (!properties)
return null;
let value = get(properties, propertyPath, defaultValue);
value = converterFunction
? converterFunction(value)
: values;
return { ...defaultValue, ...value };
}, [properties, propertyPath, defaultValue, converterFunction]);
return propertyValue;
};
//# sourceMappingURL=usePropertyValue.js.map