Answers for "async code inside useeffect"

1

async useeffect

useEffect(() => {
  (async function anyNameFunction() {await loadContent();})();
}, []);
Posted by: Guest on February-24-2021
0

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>
  )
}
Posted by: Guest on October-13-2020
0

async in useeffect

useEffect(() => {
    (async () => {
      const products = await api.index()
      setFilteredProducts(products)
      setProducts(products)
    })()
  }, [])
Posted by: Guest on May-12-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language