Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react useeffect async

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

async in useeffect

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

Comment

async setstate useeffect

const [users, setUsers] = useState([]);
  
useffect(() => {
  const getUsers = async () => {
    let response = await fetch('/users');
    let data = await response.json();
    setUsers(data);
  };
    
 getUsers();
}, []);
Comment

Using async in UseEffect

useEffect(() => {
  const getUsers = async () => {
    const users = await fetchUsers();
    setUsers(users);
  };

  getUsers(); // run it, run it

  return () => {
    // this now gets called when the component unmounts
  };
}, []);
Comment

async in useeffect

function myApp() {
  const [data, setdata] = useState()

  useEffect(() => {
    async function fetchMyAPI() {
      const response = await fetch('api/data')
      response = await response.json()
      setdata(response)
    }

    fetchMyAPI()
  }, [])
}
Comment

async useeffect

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

using async function in useEffect

function Example() {
  const [data, dataSet] = useState<any>(null)

  useEffect(() => {
    async function fetchMyAPI() {
      let response = await fetch('api/data')
      response = await response.json()
      dataSet(response)
    }

    fetchMyAPI()
  }, [])

  return <div>{JSON.stringify(data)}</div>
}
Comment

async useEffect

 function OutsideUsageExample() {
  const [data, dataSet] = useState<any>(null)

  const fetchMyAPI = useCallback(async () => {
    let response = await fetch('api/data')
    response = await response.json()
    dataSet(response)
  }, [])

  useEffect(() => {
    fetchMyAPI()
  }, [fetchMyAPI])

  return (
    <div>
      <div>data: {JSON.stringify(data)}</div>
      <div>
        <button onClick={fetchMyAPI}>manual fetch</button>
      </div>
    </div>
  )
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: find Array of value in JSON 
Javascript :: javascript check typeof array 
Javascript :: semantics ui complete responsive menu 
Javascript :: datetime knex 
Javascript :: js navigate to anchor 
Javascript :: javascript round to 1 decimal 
Javascript :: how to use ctx on canvas js 
Javascript :: (node:5547) UnhandledPromiseRejectionWarning: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" 
Javascript :: javascript onsubmit 
Javascript :: how to find the smallest two numbers in an array javascript 
Javascript :: sort string 2d array in javascript 
Javascript :: js trigger window resize 
Javascript :: jsp include html 
Javascript :: ACCESS IFRAME INNER HTML IN CHROME CONSOLE 
Javascript :: i18n react meta description 
Javascript :: json enum string 
Javascript :: how to use url parameters in react 
Javascript :: jquery option selected 
Javascript :: get date javascript 
Javascript :: js get random hex color 
Javascript :: window.location.search get parameters react 
Javascript :: $(this).text() in jquery return white space 
Javascript :: connect mongoose from node js 
Javascript :: angular add a new line from component 
Javascript :: getting days difference with moment js 
Javascript :: discord.js remove reaction 
Javascript :: Loop array backwards in JS 
Javascript :: jquery wp disable 
Javascript :: how to check if a string contains a certain letter in js 
Javascript :: check if all elements in array are true javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =