You have mentioned, you can't call a hook (like useEffect) in an ordinary helper function that is declared outside of a component
But we can call hook in any Plain JS function rite
No you can't call hooks in a plain JavaScript function either outside the component function or inside
import { useState, useEffect } from "react"; const CustomUtil = (url) => { const [data, setData] = useState(null); useEffect(() => { fetch(url) .then((res) => res.json()) .then((data) => setData(data)); }, [url]); return [data]; }; export default CustomUtil; `this is a plain function with hooks in it
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You have mentioned, you can't call a hook (like useEffect) in an ordinary helper function that is declared outside of a component
But we can call hook in any Plain JS function rite
No you can't call hooks in a plain JavaScript function either outside the component function or inside