0

Expected effect: In componentDidMount () I download s and saves in the variabletimeId. If timeId is true, passthis.state.timeId to the loadTime () function to https://app/load-id/${id} and call this function. The data returned by this function is saved in the variable checkId. this.state.checkId transfers to theDetails component. Problem: how to call the function loadId (), after receiving data in componentDidMount ()?

App

class App extends Component {
  constructor (props) {
    super(props);
    this.state = {
      checkId: '',
      timeId: ''
    }
  }

    componentDidMount() {
        axios({
            url: `https://app`,
            method: "GET",
            headers: {
              'Authorization': `Bearer ${token}`
            }
        })
        .then(res => {
            this.setState({
              timeId: res.data.id,
            });
        })
        .catch(error => {
            console.log(error);
        }) 

    }

  loadId =  (id) => {  //id ---> this.state.timeId
    axios({
        url: `https://app/load-id/${id}`,   
        method: "GET",
        headers: {
          'Authorization': `Bearer ${token}`           
        }
    })
    .then(response => {
        console.log(response);
        this.setState({
            checkId: response.data
        });
    })
    .catch(error => {
        console.log(error);
    })
}



  render () {

    return (
        <div>
            <Item

            />

            <Details 
                checkId = {this.state.checkId}  
            />
        </div> 
    )
  }
}

Details

class Details extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
        task: ''
    };
  }


  componentDidUpdate(previousProps, previousState) {
    if (previousProps.checkId !== this.props.checkId) {

        this.setState({
            task: this.props.checkId
        })
    }

  render() {
    return (
        <div >

        </div>      
    );
  }
}
Umbro
  • 1,984
  • 12
  • 40
  • 99

2 Answers2

1

You need to call loadId inside the then function.

    axios({
        url: `https://app`,
        method: "GET",
        headers: {
          'Authorization': `Bearer ${token}`
        }
    })
    .then(res => {
        this.setState({
          timeId: res.data.id,
        });
        this.loadId(res.data.id);
    })
    .catch(error => {
        console.log(error);
    }) 
Philip Moy
  • 481
  • 3
  • 6
0

You need to bind loadId() to set state and call it when request in componentDidMount() returns response:

class App extends Component {
  constructor (props) {
    super(props);
    this.state = {
      checkId: '',
      timeId: ''
    }

    this.loadId = this.loadId.bind(this); // Bind here
  }

    componentDidMount() {
        axios({
            url: `https://app`,
            method: "GET",
            headers: {
              'Authorization': `Bearer ${token}`
            }
        })
        .then(res => {
            this.setState({
              timeId: res.data.id,
            });
            this.loadId(res.data.id); // Call loadId
        })
        .catch(error => {
            console.log(error);
        }) 

    }

  //...
}


ThanhPhanLe
  • 1,315
  • 3
  • 14
  • 25