UNPKG

next

Version:

The React Framework

56 lines (55 loc) 2.57 kB
import { Children, useEffect, useLayoutEffect } from 'react'; const isServer = typeof window === 'undefined'; const useClientOnlyLayoutEffect = isServer ? ()=>{} : useLayoutEffect; const useClientOnlyEffect = isServer ? ()=>{} : useEffect; export default function SideEffect(props) { const { headManager, reduceComponentsToState } = props; function emitChange() { if (headManager && headManager.mountedInstances) { const headElements = Children.toArray(Array.from(headManager.mountedInstances).filter(Boolean)); headManager.updateHead(reduceComponentsToState(headElements, props)); } } if (isServer) { var _headManager_mountedInstances; headManager == null ? void 0 : (_headManager_mountedInstances = headManager.mountedInstances) == null ? void 0 : _headManager_mountedInstances.add(props.children); emitChange(); } useClientOnlyLayoutEffect(()=>{ var _headManager_mountedInstances; headManager == null ? void 0 : (_headManager_mountedInstances = headManager.mountedInstances) == null ? void 0 : _headManager_mountedInstances.add(props.children); return ()=>{ var _headManager_mountedInstances; headManager == null ? void 0 : (_headManager_mountedInstances = headManager.mountedInstances) == null ? void 0 : _headManager_mountedInstances.delete(props.children); }; }); // We need to call `updateHead` method whenever the `SideEffect` is trigger in all // life-cycles: mount, update, unmount. However, if there are multiple `SideEffect`s // being rendered, we only trigger the method from the last one. // This is ensured by keeping the last unflushed `updateHead` in the `_pendingUpdate` // singleton in the layout effect pass, and actually trigger it in the effect pass. useClientOnlyLayoutEffect(()=>{ if (headManager) { headManager._pendingUpdate = emitChange; } return ()=>{ if (headManager) { headManager._pendingUpdate = emitChange; } }; }); useClientOnlyEffect(()=>{ if (headManager && headManager._pendingUpdate) { headManager._pendingUpdate(); headManager._pendingUpdate = null; } return ()=>{ if (headManager && headManager._pendingUpdate) { headManager._pendingUpdate(); headManager._pendingUpdate = null; } }; }); return null; } //# sourceMappingURL=side-effect.js.map