本站由axios爱好者共建,部署在vultr vps上,推荐使用vultr!价格实惠,实力雄厚。 最近新注册用户充值$25,可额外获赠$50,搭建博客必备。 前往注册
udemy web开发课程,涵盖angular、vue和react,共17门,几十G课程网盘观看 前往领取
react-axios
Axios Component for React with child function callback.
This is intended to allow in render async requests.
Features
- Same great features found in Axios
- Component driven
- Child function callback (error, response, isLoading, onReload) => { }
- Auto cancel previous requests
- Debounce to prevent rapid calls.
- Request only invoked on prop change and isReady state.
- Callback props for onSuccess, onError, and onLoading
- Supports custom axios instances through props or a <AxiosProvider … >
Installing
Using npm:
$ npm install react-axios |
Also install the required peer dependancies if you have not already done so:
$ npm install axios |
Components & Properties
Base Request Component
<Request |
Helper Components
<Get ... /> |
Example
Include in your file
import { AxiosProvider, Request, Get, Delete, Head, Post, Put, Patch, withAxios } from 'react-axios' |
Performing a GET
request
// Post a request for a user with a given ID |
Exposed properties on the child function.
error
The error object returned by Axios.
response
The response object returned by Axios.
isLoading
Boolean flag indicating if Axios is currently making a XHR request.
onReload(props)
Function to invoke another XHR request. This function accepts new temporary props that will be overloaded with the existing props for this request only.
Custom Axios Instance
Create an axios instanceconst axiosInstance = axios.create({
baseURL: '/api/',
timeout: 2000,
headers: { 'X-Custom-Header': 'foobar' }
});
Pass down through a provider<AxiosProvider instance={axiosInstance}>
<Get url="test">
{(error, response, isLoading, onReload) => {
...
}}
</Get>
</AxiosProvider>
Or pass down through props<Get url="test" instance={axiosInstance}>
{(error, response, isLoading, onReload) => {
...
}}
</Get>
Retrieve from custom provider (when you need to directly use axios).
The default instance will be passed if not inside an <AxiosProvider/>
.const MyComponent = withAxios(class MyComponentImpl extends React.Component {
componentWillMount() {
this.props.axios('test').then(result => {
this.setState({ data: result.data })
})
}
render() {
const data = (this.state || {}).data
return <div>{JSON.stringify(data)}</div>
}
})
<AxiosProvider instance={axiosInstance}>
<MyComponent/>
</AxiosProvider>