Akila

Akila

  • NA
  • 216
  • 33.9k

useeffect call when state changes in react

Dec 17 2021 4:21 AM

I want to call notification api when the notification count change.

const [data, setData] = useState('');

  const [filteredDataSource, setFilteredDataSource] = useState([]);

  useEffect(()=>{

    getData({});     

  },[filteredDataSource]); 

  const getData = async () => {

     axios

      .get(`${BASE_URL}/Notification/getnotification`, {

        headers: {

          Authorization: `bearer ${accesstoken}`,

        },

      })

      .then(({data}) => {

               setData(data['0'].NotificationCount);

              setFilteredDataSource(data);

           });

 

this is working but every time the api was called.its make my app slow.only i want when the count increase or decrese then api call


Answers (1)