Akila

Akila

  • NA
  • 216
  • 33.9k

callback on state change react

Oct 27 2021 12:00 PM

HI,

I want to show notification count.in useeffect only i can show that.but useeffect make function slow. how can i use callback when the state change.

 

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

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

  useEffect(()=>{

    getData({});

    notify();

  },[filteredDataSource]);

  const notify = useCallback(() => {

    getData({});

  }, [filteredDataSource])

  const getData = async () => {

        axios

      .get(url, {

             })

      .then(({data}) => {

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

             setFilteredDataSource(data);

             });

   };


Answers (1)