How to call an async function inside a UseEffect() in React?

Yassine Layachi

I would like to call an async function and get the result for my UseEffect.

The fetch api examples i found on the internet are directly made in the useEffect function. If my URL changes, i must patch all my fetchs.

When i tried, i got an error message.

This is my code.


    async function getData(userId) {
        const data = await axios.get(`http://url/api/data/${userId}`)
            .then(promise => {
                return promise.data;
            })
            .catch(e => {
                console.error(e);
            })
            return data;
    }


    function blabla() {
        const [data, setData] = useState(null);

        useEffect(async () => {
            setData(getData(1))
        }, []);

        return (
            <div>
                this is the {data["name"]}
            </div>
        );
    }

index.js:1375 Warning: An effect function must not return anything besides a function, which is used for clean-up. It looks like you wrote useEffect(async () => ...) or returned a Promise. Instead, write the async function inside your effect and call it immediately:

useEffect(() => {
  async function fetchData() {
    // You can await here
    const response = await MyAPI.getData(someId);
    // ...
  }
  fetchData();
}, [someId]); // Or [] if effect doesn't need props or state
Fraction

Create an async function inside your effect that wait the getData(1) result then call setData():

useEffect(() => {
  const fetchData = async () => {
     const data = await getData(1);
     setData(data);
  }

  fetchData();
}, []);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to call loading function with React useEffect only once

From Java

How to Call a Function inside a Render in React/Jsx

From Dev

How to call a function which is inside a class in React?

From Dev

How to call function after completion of async functions inside loop?

From Dev

How to call function after completion of async functions inside loop?

From Dev

How to call async.each in an external function having async.waterfall inside async.each

From Dev

Call async function inside non-async function

From Dev

Function call inside a $.each loop, is it async or sync?

From Dev

Function call inside a $.each loop, is it async or sync?

From Java

React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing

From Dev

How to call the async Task Function?

From Dev

call react function inside anonymous function

From Dev

How to call a function inside SpookyJS?

From Dev

How to call function inside the IIFE

From Dev

How to call a function inside constructor

From Dev

how to call function inside a trigger?

From Dev

How to call a function inside a class

From Dev

How to call a function inside SpookyJS?

From Dev

How to call function inside the IIFE

From Dev

How to call a function inside an iframe

From Dev

call function inside of className react.js

From Dev

Node.js: Returning value from function with async call inside

From Dev

Why is async required to call await inside a JavaScript function body?

From Dev

Why is async required to call await inside a JavaScript function body?

From Dev

Node.js: Returning value from function with async call inside

From Dev

Async call inside loop

From Dev

How to use 'yield' inside async function?

From Dev

How can I call a function inside of a function?

From Dev

How to call function inside a main function in MATLAB?

Related Related

  1. 1

    How to call loading function with React useEffect only once

  2. 2

    How to Call a Function inside a Render in React/Jsx

  3. 3

    How to call a function which is inside a class in React?

  4. 4

    How to call function after completion of async functions inside loop?

  5. 5

    How to call function after completion of async functions inside loop?

  6. 6

    How to call async.each in an external function having async.waterfall inside async.each

  7. 7

    Call async function inside non-async function

  8. 8

    Function call inside a $.each loop, is it async or sync?

  9. 9

    Function call inside a $.each loop, is it async or sync?

  10. 10

    React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing

  11. 11

    How to call the async Task Function?

  12. 12

    call react function inside anonymous function

  13. 13

    How to call a function inside SpookyJS?

  14. 14

    How to call function inside the IIFE

  15. 15

    How to call a function inside constructor

  16. 16

    how to call function inside a trigger?

  17. 17

    How to call a function inside a class

  18. 18

    How to call a function inside SpookyJS?

  19. 19

    How to call function inside the IIFE

  20. 20

    How to call a function inside an iframe

  21. 21

    call function inside of className react.js

  22. 22

    Node.js: Returning value from function with async call inside

  23. 23

    Why is async required to call await inside a JavaScript function body?

  24. 24

    Why is async required to call await inside a JavaScript function body?

  25. 25

    Node.js: Returning value from function with async call inside

  26. 26

    Async call inside loop

  27. 27

    How to use 'yield' inside async function?

  28. 28

    How can I call a function inside of a function?

  29. 29

    How to call function inside a main function in MATLAB?

HotTag

Archive