skip to Main Content
useEffect(() => {
  const getCategory = () => {
    allcategory().then((res) => {
      if (res.data.error) {
        console.log("error");
        setValues({ ...values,
          message: res.data.error
        });
      } else {
        setValues({ ...values,
          categories: res.data.category,
          formData: new FormData(),
        })
      }
    })
  }

  const getPublisher = () => {
    allpublisher().then((res) => {
      if (res.data.error) {
        console.log("error");
        setValues({ ...values,
          message: res.data.error
        });
      } else {
        setValues({ ...values,
          publishers: res.data.publisher,
          formData: new FormData(),
        });
      }
    })
  }
  getCategory()
  getPublisher()
}, [])

When I try to add two function on useEffect it doesn’t render any data but show data on first render if only one function is present on useEffect

2

Answers


  1. I think you miss async function.
    I think this should like this

    const getCategory = async () =>

    const getPublisher = async () =>

    And with my opinion, you should refractor like this:

    const getCategory = async () => {
     // ...do sth
    }
    
    const getPublisher = async () => {
     // ...do sth
    }
    
    useEffect(() => {
     getCategory();
     getPublisher();
    },[])
    
    Login or Signup to reply.
  2. Since you are using promises, you can implement promise chaining here like

    getCategory().then(()=>getPublisher()) 
    

    But instead of having multiple promise chains, you can implement async await functions

    const getData = async() => {
     await getCategory()
     await getPublisher()
    }
    
    useEffect(() => {
    getData()
    },[])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search