Home > Blockchain >  Why useState is not accepting input received from the GET request?
Why useState is not accepting input received from the GET request?

Time:05-03

I am trying to get an object from the server in the form {Name: true, Place: false, Animal: true, Thing: true} save this data into categoryDetail then extract it using categoryDetail.Name and then pass it to the useState. But somehow useState is not accepting this data.

Here is the code:

const[categoryDetail, setCategoryDetail]=useState({});
    
    useEffect(()=>{
        axios.get('http://localhost:8080/feeds/category')
            .then(response=>{
               
                if (JSON.stringify(categoryDetail) !== JSON.stringify(response.data.category)) {
                    
                    setCategoryDetail(response.data.category);
                }
                
            })
            .catch(err=>{
                console.log(err);
            })
    })


    
    console.log(categoryDetail.Name);  //            
  • Related