@adaptabletools/adaptable-cjs
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
44 lines (43 loc) • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAdaptableState = void 0;
const react_1 = require("react");
const AdaptableContext_1 = require("../../View/AdaptableContext");
function useAdaptableState(selector) {
const adaptable = (0, AdaptableContext_1.useAdaptable)();
const store = adaptable.adaptableStore;
const [_refreshKey, setRefreshKey] = (0, react_1.useState)(0);
const resultRef = (0, react_1.useRef)(null);
const getResult = () => {
if (!store) {
return null;
}
const state = store.getCurrentStorageState();
if (!state) {
return null;
}
return selector ? selector(state) : state;
};
// don't refresh when the state changes
// as we're under the adaptable context
// and this means it will be refreshed automatically
// useEffect(() => {
// if (!store) {
// return;
// }
// return store.onAny(() => {
// const newResult = getResult();
// if (newResult !== resultRef.current) {
// //only refresh when the result has changed
// setRefreshKey((prev) => prev + 1);
// }
// });
// }, [store]);
if (!adaptable || !adaptable.isReady) {
return null;
}
const res = getResult();
resultRef.current = res;
return res;
}
exports.useAdaptableState = useAdaptableState;