Created
May 25, 2022 10:36
-
-
Save dektaiimage/6e9a5fd6ef02ea87028ee97c0cf69ee1 to your computer and use it in GitHub Desktop.
Example React Custom Hooks
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 React, { useState, useEffect } from 'react'; | |
import axios from 'axios'; | |
export default function useFetch(url){ | |
const [data, setData] = useState([]); | |
useEffect(() => { | |
const fetchData = async () => { | |
const { data } = await axios.get(url) | |
setPosts(data); | |
} | |
fetchData(); | |
},[url]) | |
return{ | |
data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment