data/projects.jsx
import useSWR from 'swr'
// ----------------------------------------
import { API } from '../tools'
const fetcher = (...args) => API(...args)
function useProjects ( action ) {
const { data, error } = useSWR(action, fetcher)
//const { data, error } = useSWR(action, fetcher, { refreshInterval: 1000 })
return {
getProjects : (args)=>{return fetcher(args)},
projects : data || [],
projects_loading : !error && !data,
projects_error : error,
}
}
export default useProjects
App.jsx
import useProjects from './data/projects'
function App() {
const { getProjects } = useProjects()
const [projects, setProjects] = React.useState([]);
const action = {
method: 'GET',
domain: 'projects',
}
// ----------------------------------------
React.useEffect(() => {
getProjects( action ).then(data =>{
setPrj( data )
});
}, [] )
// -----------------------
React.useEffect(() => {
console.log('projects', projects)
}, [ projects ] )
..............