A tiny higher order component to manage AJAX requests in React components.
- Automatically aborts requests on
componentWillUnmount
. - Supports many AJAX clients.
- Exposes components
pending
state as a property. - Abort all pending requests at will.
import React from 'react';
import jax from 'react-jax';
import superagent from 'superagent';
@jax(superagent)
export default class MyComponent extends React.Component {
sendRequest = () => {
this.props.get('https://example.com').end((err, res) => {
// your code
});
}
render() {
return this.props.pending ?
<button onClick={this.sendRequest}>Click Me</button> :
<button onClick={this.props.abort}>Cancel</button>;
}
}
@jax(options)
export default class Test extends React.Component {
/* your code */
}
class Test extends React.Component {
/* your code */
}
export default jax(options)(Test);
You can choose to pass a superagent client or an object for additional options.
These options can be passed to the jax()
decorator.
Array of jax methods to expose as properties.
Property name to expose the pending status as.
Property name to expose the abort function as.
Events emitted by the clients request object than indicate it should be cleaned up.
Aborts all pending requests sent by the component.
Returns true if any request sent by the component are pending.
Exact same function signature the client
exposes.
For example, superagent will expose functions like these.