React Hook “useState“ is called in function “xxx“ which is neither a React function component or

Error code

import {useState,useEffect} from 'react'
const useData = () => {
    const [data,setData] = useState({count:0});
    useEffect(()=>{
        setTimeout(()=> {
            setData((data)=>({...data,count:data.count+1}))
        }, 1000 );
    },[])
    return {data}
}
 
export default useData

Modified code

import React from 'react'
import useData from '../hooks/AppData'
const AppData = ()=>{
    const {data} = useData()
    return <div>
    count->{data.count}
    </div>
}
export default AppData; // Change the first letter of the name to uppercase

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *