useeffect react api call async code example

Example 1: react useeffect async

const MyFunctionnalComponent: React.FC = props => {
  useEffect(() => {
    // Using an IIFE
    (async function anyNameFunction() {
      await loadContent();
    })();
  }, []);  
  
  return <div></div>;
};

Example 2: async useeffect

useEffect(() => {
  (async function anyNameFunction() {await loadContent();})();
}, []);

Example 3: async in useeffect

useEffect(() => {
    (async () => {
      const products = await api.index()
      setFilteredProducts(products)
      setProducts(products)
    })()
  }, [])