I am learning to make react custom hook. I made a folder named hooks in src folder. when trying to use useState and useEffect in my code, it's not getting imported but says require. useState and useEffect getting error and red undeline.
I was using the hooks inside an arrow function. Here's my code:
const { useState, useEffect } = require("react");
const userProducts = () => {
const [products, setProducts] = useState([])
useEffect(()=>{
fetch("products.json")
.then(res=>res.json())
.then(data=>setProducts(data))
},[])
return [products, setProducts];
};
export default userProducts;
CodePudding user response:
you need to import the useState & useEffect from react. and make sure that you use declare function under the main function.
CodePudding user response:
Try this on first line:
import React, { useState, useEffect } from 'react';