Git Product home page Git Product logo

react-jax's Introduction

react-jax Build Status

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.

Example

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>;
    }
}

API

As a decorator
@jax(options)
export default class Test extends React.Component {
    /* your code */
}
As a function
class Test extends React.Component {
    /* your code */
}

export default jax(options)(Test);

Options

You can choose to pass a superagent client or an object for additional options.

These options can be passed to the jax() decorator.

client required
methods defaults to ['get', 'post', 'del', 'put']

Array of jax methods to expose as properties.

pendingKey defaults to pending

Property name to expose the pending status as.

abortKey defaults to abort

Property name to expose the abort function as.

endEvents defaults to ['end', 'abort', 'error']

Events emitted by the clients request object than indicate it should be cleaned up.

Properties

props[abortKey]() -> undefined

Aborts all pending requests sent by the component.

props[pendingKey] -> boolean

Returns true if any request sent by the component are pending.

props[method](...args) -> req

Exact same function signature the client exposes. For example, superagent will expose functions like these.

react-jax's People

Contributors

eng-svc-terminus avatar nickclaw avatar zyadelgohary-okta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.