I want to use my custom hook inside a regular function as follows, but React won't let me do that. I wonder if my useUrl custom hook is not valid or there is something wrong with calling the hook.
const handleAddFormation = async () => {
console.log('add formation')
await useUrl('POST', 'http://localhost:1337/formations', newFormationList)
setNewFormationList([])
setModalOpen(false)
}
My custom hook:
export default function useUrl(method, url, args) {
const [data, setData] = useState([])
const [loading, setLoading] = useState(true)
useEffect(() => {
setLoading(true)
axios({
method,
url,
data: args
})
.then((res) => {
setData(res.data)
setLoading(false)
})
.catch((err) => {
console.log(err)
setLoading(false)
})
}, [])
if (!loading) return data
}
This gives me the following error:
React Hook "useUrl" is called in function "handleAddFormation" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter
handleAddFormationis neither of those .