File size: 1,362 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import index from 'instantsearch.js/es/widgets/index/index';
import { useMemo } from 'react';
import { useIndexContext } from '../lib/useIndexContext';
import { useForceUpdate } from './useForceUpdate';
import { useInstantSearchServerContext } from './useInstantSearchServerContext';
import { useInstantSearchSSRContext } from './useInstantSearchSSRContext';
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
import { useStableValue } from './useStableValue';
import { useWidget } from './useWidget';
import type { IndexWidgetParams } from 'instantsearch.js/es/widgets/index/index';
export type UseIndexProps = IndexWidgetParams;
export function useIndex(props: UseIndexProps) {
const serverContext = useInstantSearchServerContext();
const ssrContext = useInstantSearchSSRContext();
const initialResults = ssrContext?.initialResults;
const parentIndex = useIndexContext();
const stableProps = useStableValue(props);
const indexWidget = useMemo(() => index(stableProps), [stableProps]);
const helper = indexWidget.getHelper();
const forceUpdate = useForceUpdate();
useIsomorphicLayoutEffect(() => {
forceUpdate();
}, [helper, forceUpdate]);
useWidget({
widget: indexWidget,
parentIndex,
props: stableProps,
shouldSsr: Boolean(serverContext || initialResults),
});
return indexWidget;
}
|