ayanami
Version:
A better way to react with state
20 lines (19 loc) • 1.01 kB
JavaScript
import * as React from 'react';
import get from 'lodash/get';
import { getInstanceWithScope, TransientScope, createScopeWithRequest, } from '../core';
import { DEFAULT_SCOPE_NAME } from '../ssr/constants';
import { isSSREnabled } from '../ssr/flag';
import { SSRContext } from '../ssr/ssr-context';
import { useAyanamiInstance, } from './use-ayanami-instance';
export function useAyanami(A, config) {
var scope = get(config, 'scope');
var selector = get(config, 'selector');
var req = isSSREnabled() ? React.useContext(SSRContext) : null;
var reqScope = req ? createScopeWithRequest(req, scope) : scope;
var ayanami = React.useMemo(function () { return getInstanceWithScope(A, reqScope); }, [reqScope]);
ayanami.scopeName = scope || DEFAULT_SCOPE_NAME;
var useAyanamiInstanceConfig = React.useMemo(function () {
return { destroyWhenUnmount: scope === TransientScope, selector: selector };
}, [reqScope]);
return useAyanamiInstance(ayanami, useAyanamiInstanceConfig);
}