Last active
April 12, 2021 13:08
-
-
Save Karnak19/ec31ae291c9345efe14c98c38a78fa47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BASE_URL = 'https://jsonplaceholder.typicode.com'; | |
export default { | |
TODOS_URL: `${BASE_URL}/todos`, | |
USERS_URL: `${BASE_URL}/users`, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from 'react'; | |
import axios from 'axios'; | |
import { TODOS_URL } from './api'; | |
export function useGetAll() { | |
const [todos, setTodos] = useState([]); | |
const [error, setError] = useState(null); | |
const [loading, setLoading] = useState(true); | |
useEffect(() => { | |
const getAll = async () => { | |
setError(null); | |
setLoading(true); | |
try { | |
const { data } = await axios.get(TODOS_URL); | |
setTodos(data); | |
} catch (error) { | |
setError(error); | |
} finally { | |
setLoading(false); | |
} | |
}; | |
getAll(); | |
}, []); | |
return { todos, error, loading }; | |
} | |
export function useGetOne(id) { | |
const [todo, setTodo] = useState({}); | |
useEffect(() => { | |
const getOne = async () => { | |
const { data } = await axios.get(`${TODOS_URL}/${id}`); | |
setTodo(data); | |
}; | |
getOne(); | |
}, []); | |
return todo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment