@fakel/rest-admin
Version:
An application that makes it easier to work with your API
34 lines (27 loc) • 853 B
text/typescript
import { useState, useEffect } from 'react';
import { useDataProviderStore } from './useDataProviderStore';
import { useResourceStore } from './useResourceStore';
export const useInitialValue = (id: string) => {
const [initialValue, setInitialValue] = useState();
const [loading, setLoading] = useState(false);
const dataProviderStore = useDataProviderStore();
const resourceStore = useResourceStore();
const { dataProvider } = dataProviderStore;
const getInitialData = () => {
if (resourceStore.current) {
setLoading(true);
dataProvider
.getOne(resourceStore.current, {
id,
})
.then((response) => {
setInitialValue(response.data);
setLoading(false);
});
}
};
useEffect(() => {
getInitialData();
}, []);
return { initialValue, loading };
};