score:2

Accepted answer

just call the second function in the then() block of the first and pass the data as a param. setstate is asynchronous so you can't rely on the data to be set immediately.

fetchduel = () => {
        const duelid = this.props.match.params.duelid;
        axios.get(`http://127.0.0.1:8000/api/duel/${duelid}`,
            {'headers': {'authorization': `token                                ${localstorage.getitem('token')}`}})
            .then(res => {
                this.setstate({
                    duel: res.data
                });
                this.fetchdataset(res.data);
            });
    };

score:0

just call the second function in the .then() of the first function using data from the response. example:

class dueldetail extends react.component {
    state = {
        duel: [],
        dataset: null
    };


    fetchduel = () => {
        const duelid = this.props.match.params.duelid;
        axios.get(`http://127.0.0.1:8000/api/duel/${duelid}`,
            {'headers': {'authorization': `token ${localstorage.getitem('token')}`}})
            .then(res => {
                this.setstate({
                    duel: res.data
                });
                
                this.fetchdataset(res.data.dataset) 
                // pass whatever property you get from the response here.
            });
    };

    fetchdataset = (datasetid) => {
        axios.get(`http://127.0.0.1:8000/api/dataset/${datasetid}`,
            {'headers': {'authorization': `token ${localstorage.getitem('token')}`}})
            .then(res => {
                this.setstate({
                    dataset: res.data
                });
            });
    };


    componentdidmount() {
        this.fetchduel()
    }

score:1

as the 2 actions are async you need to handle it accordingly.

axios get returns a promise .so you can call the second action in the then block of the first action.

also, setstate is an aync action.(it gets queued up and doesn't get triggered instantly). use the data received from the first action, in its then block, pass it to the second action


Related Query

More Query from same tag