how to fetch data from api using axios in react function components code example
Example: how can we fetch data from api in using axios react
const users = data.map(u =>
{u.id}
{u.name}
{u.email}
{u.website}
{u.company.name}
)
this.setState({users})
import React, {Component} from 'react'
import axios from '../../axios'
export default class users extends Component {
constructor(props) {
super(props);
this.state = {
Users: []
};
}
getUsersData() {
axios
.get(`/users`, {})
.then(res => {
const data = res.data
console.log(data)
const users = data.map(u =>
{u.id}
{u.name}
{u.email}
{u.website}
{u.company.name}
)
this.setState({
users
})
})
.catch((error) => {
console.log(error)
})
}
componentDidMount(){
this.getUsersData()
}
render() {
return (
{this.state.users}
)
}
}