how to install axios in react
$ yarn add axios
how to install axios in react
$ yarn add axios
how can we fetch data from api in using axios react
const users = data.map(u =>
<div>
<p>{u.id}</p>
<p>{u.name}</p>
<p>{u.email}</p>
<p>{u.website}</p>
<p>{u.company.name}
</div>
)
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 =>
<div>
<p>{u.id}</p>
<p>{u.name}</p>
<p>{u.email}</p>
<p>{u.website}</p>
<p>{u.company.name}</p>
</div>
)
this.setState({
users
})
})
.catch((error) => {
console.log(error)
})
}
componentDidMount(){
this.getUsersData()
}
render() {
return (
<div>
{this.state.users}
</div>
)
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us