Makes the superagent ajax calls available to the react component hierarchy
npm install react-context-ajaxThis is react wrapper on superagent.
It is component which should be at top of component hierarchy. It make ajax calls available in all child components.
It allow to config it globally and/or in each call.
npm install react-context-ajax
It needs react to work correctly. It is compiled without react at its source.
Simplest usage with pure react, with ES6/ES7 syntax and jsx (In next sections it will also be proposition how to usage this with redux, react, react-router)
Main root jsx file app.jsx :
import React from 'react';
import ReactDOM from 'react-dom';
import Request from 'react-context-ajax';
import SomeComponent from './someComponent';
class App extends React.Component {
constructor(props) {
super(props);
};
render() {
return (
);
}
}
ReactDOM.render(
document.getElementById('app')
);
As you can see you have to do two steps in main file:
1. import react-context-ajax:
import Request from 'react-context-ajax';
2. Wrap your render jsx components by 'react-context-ajax'(
class App extends React.Component {
//...
render() {
return (
//your normal components
);
}
Now all components inside 'react-context-ajax' (
If your component need access to router you need to set contextTypes.
Example component someComponent.jsx:
class SomeComponent extends React.Component {
static contextTypes = {
request: React.PropTypes.object
};
constructor(props) {
super(props);
};
componentWillMount() {
this.context.request.getRequest({
url: 'http://yourUrl.com/someParam',
query: {
key: 'value'
},
endCallback: (err, req, res)=>{
//response from server
}
});
}
//...
}
As you can see you have to inform component what context take from parent components:
static contextTypes = {
request: React.PropTypes.object
};
And now you can use router
this.context.request
headers={this.headerObject}
endCallback={this.globalCallbackFunction}
>
// children components
* type: string
* required: false
* default: ''
First part of url, where request are send. If all your request have similar part then this part can be set here.
* type: object
* required: false
Allow to set headers for all request methods. There is also possibility to change global headers using request instance.
Run after component componentDidMount phase and pass to callback request instance.
* type: function
* required: false
This is global endCallback which is called after ech request from child components. It is not required. You can handle all
requests in local callback (passed as parameter to request call). More about it in section Global endCallback
Request object is available in components content:
this.context.request
And it has methods:
Return superagent instance.
Add next global header (for all request). If user is login you send for each request token in header.
Remove one global header (for all request). For example after user logout we don't want to send token in header
#### options.url
* type: string
* required: false
A string containing the URL to which the request is sent. It is concatenate with baseUrl
Example:
this.context.request.getRequest({
url: 'someurl',
})
#### options.query
* type: object
* default: {}
* required: false
Data that is added as query to url
Example:
this.context.request.postRequest({
url: 'someurl',
query: {
id: '213241421
}
})
#### options.body
* type: object
* default: {}
* required: false
Data that is sent to the server with the request. Not in use for get and delete methods.
Example:
this.context.request.postRequest({
url: 'someurl',
body: {
name: '213241421,
something: 'fdsfdsds'
}
})
#### options.headers
* type: object
* default: {}
* required: false
Set headers for this request. If header has same key as globally set header, then this header will override it.
If you don't want to sent in this request any global header (but you don't want to remove them globally) you can
set this header here with value null
Example:
this.context.request.postRequest({
url: 'someurl',
headers: {
'access-token', '1212414fsvsvsdsvdsvsd
},
body: {
name: '213241421,
something: 'fdsfdsds'
}
})
#### options.fields
* type: object
* default: {}
* required: false
For Multipart requests. Much like form files inputs in HTML, you can send files to server.
Example:
this.context.request.postRequest({
url: 'someurl',
attachments: {
file1: 'path/to/myfile.ext',
},
fields: {
field1: 'dsadsa',
field1: 'vxcxcvxcv'
}
})
#### options.attachments
* type: object
* default: {}
* required: false
For Multipart requests. Much like form fields in HTML, you can set field values with the fields = { name1: value1, name2: value2 }
Example:
this.context.request.postRequest({
url: 'someurl',
attachments: {
file1: 'path/to/myfile.ext',
file2: {
path: 'path/to/secondfile.ext',
},
file3: {
path: 'path/to/thirdfile.ext',
filename: 'newname.ext2'
},
}
})
#### options.endCallback
Callback invoked after response from server, error or timeout. More about it in section Local endCallback
#### options.name
You can set identify string for each call. Then in global callback you can check req.name to identify which call is processed.
#### options.*
You can add any custom property. All properties will be available in callbacks req parameter.
To abort request you have to use superagent request object (this object is returned from all request methods) and invoke its abort() method.
example:
var request = this.context.request.getRequest({ ... });
request.abort();
Callback function get three arguments
globalCallbackFunction(err, req, res, done) {
done();
}
req.request
done with own error, request and response object after you process original data.
Callback function get two arguments
localCallbackFunction(err, req, res) {
done();
}
req.request