Aquí está mi código React js para una sola llamada API para un selector de rango de fechas. ahora quiero llamar a varias API en React con el método componentDidMount, ¿es posible? Si es así, ¿cómo puedo hacerlo?
import React,{ Component} from "react"; import axios from 'axios' class PostList extends Component{ constructor(props) { super(props) this.state = { posts: [] } } componentDidMount(){ axios.get('http://127.0.0.1:8000/betweendays') .then(response => { this.setState({ posts:response.data }) console.log(response.data) }) } render() { const {posts} = this.state return ( <div> <h1>get call in React js</h1> { posts.map(post => <div key = {post.id}>{post.id} </div>) } </div> ) } } export default PostList```Usando el método .then() para crear una cadena de solicitudes.
componentDidMount() { axios.get('http://127.0.0.1:8000/betweendays') .then(response => { this.setState({ posts: response.data }) return response.data; // returning response }) .then(res => { // do another request Note we have the result from the above // getting response returned before console.log(res); }) // Tag on .then here .catch(error => console.log(error)) }Puede agregar más apis en componentDidMount como desee.
componentDidMount(){ axios.get('http://127.0.0.1:8000/betweendays') .then(response => { this.setState({ posts:response.data }) console.log(response.data) }) axios.get('link') .then(response => { this.setState({ posts:response.data }) console.log(response.data) }) }