c
Size: a a a
c
c
VK
АХ
{ params //параметры, с которыми был вызван эффект, result // то, что эффект вернул } A
DS
G

c
yv
FB
c
DS
yv

yv
К
DS
К
const useData = () => {
const data = useStore($data);
const isLoading = useStore(loadDataFx.pending);
useGate(DataGate): // triggers loading if not loaded yet
return {data, isLoading}
}
DS
const createStructure = ({fxCallback, defaultState}) => {
const fx = createEffect(fxCallback)
const store = restore(fx, defaultState)
const gate = createGate()
guard({
clock: gate.open,
source: store,
filter: currentStoreState !== defaultState, // или же дугое более подходящее условие сравнения
target: fx
})
return {store, fx, gate}
}
const useStructure = ({store, fx, gate}) => {
const data = useStore(store);
const isLoading = useStore(fx.pending);
useGate(gate)
return {data, isLoading}
}
Использование
const userNameStructure = createStructure({fxCallback: () => api.getUserName(), defaultState: ''})
const User = () => {
const {data, isLoading} = useStructure(userNameStructure)
}