0

I'm trying to understand how useEffect works.

I have two callApi: "callApiDialer" is based on response of "callApiManager", for get id from list.

But "currentLeadId" state at first called obviously is null.

How can call "callApiDialer" when currentLeadId is not null?

import React, { useState, useEffect } from 'react';
    
    const [loading, setLoading] = useState(true);
    const [apiManager, setApiManager] = useState([]);
    const [apiDialer, setApiDialer] = useState([]);
    const [currentLeadId, setCurrentLeadId] = useState(null);
    
        // CALL API
        const callApiManager = async () => {
            try {
                const response = await api.get(`/api/manager/op/1`);
                setCurrentLeadId(response.data.dialer_list[0].id);
                setApiManager(response.data);
            } catch (err) {
                alert("fetchApiManager " + err.response.status);
            }
        }
    
        const callApiDialer = async () => {
            try {
                const response = await api.get(`/api/manager/lead/${currentLeadId}`);
                setApiDialer(response.data.lead);
                setLoadingModal(false);
            } catch (err) {
                alert("fetchApiSources " + err.response.status);
            }
        }
    
        useEffect(() => {
            callApiManager();
        }, [])
    
        useEffect(() => {
           console.log(currentLeadId); // <-- here get first null and after currentLeadId
           if(currentLeadId) {
             callApiDialer();
             setLoading(false);
            }
        }, [currentLeadId])
1
  • 1
    Isn't that what the code already does? I would expect if(currentLeadId) { to only execute when currentLeadId is not null. Commented Aug 26, 2022 at 11:33

2 Answers 2

1

You could have just one function that call both, therefore there would be only one useEffect.

    // CALL API
    const callBothApisAtOnce= async () => {
        try {
            const op = await api.get(`/api/manager/op/1`);
            const response = await api.get(`/api/manager/lead/${op.data.dialer_list[0].id}`);
            // rest of your logic...
        } catch (err) {
            alert("err" + err);
        }
    }

    useEffect(() => {
       callBothApisAtOnce()
    }, [])
Sign up to request clarification or add additional context in comments.

Comments

0

you can use axios's promise base functionality

axios.get(`/api/manager/op/1`).then(res => {
   setCurrentLeadId(response.data.dialer_list[0].id);
   setApiManager(response.data);
   axios.get(`/api/manager/lead/${response.data.dialer_list[0].id}`).then(res1 =>{
   setApiDialer(res1.data.lead);
   setLoadingModal(false);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.