vega-frontend-monorepo/apps/trading/hooks/react-singleton-hook/components/SingleItemContainer.js
2022-03-24 14:22:46 +01:00

20 lines
636 B
JavaScript

import { useLayoutEffect, useRef } from 'react';
export const SingleItemContainer = ({ initValue, useHookBody, applyStateChange }) => {
const lastState = useRef(initValue);
if (typeof useHookBody !== 'function') {
throw new Error(`function expected as hook body parameter. got ${typeof useHookBody}`);
}
const val = useHookBody();
//useLayoutEffect is safe from SSR perspective because SingleItemContainer should never be rendered on server
useLayoutEffect(() => {
if (lastState.current !== val) {
lastState.current = val;
applyStateChange(val);
}
}, [applyStateChange, val]);
return null;
};