Giter Site home page Giter Site logo

jackhowa / react-actioncable-provider Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cpunion/react-actioncable-provider

23.0 3.0 13.0 1.07 MB

Connect React and Action Cable, with React 16 support

Home Page: https://www.npmjs.com/package/@thrash-industries/react-actioncable-provider

License: MIT License

JavaScript 100.00%
hacktoberfest react actioncable-react actioncable rails

react-actioncable-provider's Introduction

ActionCable Provider for React

This package provides an ActionCable context provider and consumer to allow you to subscribe to ActionCable channels in your React components.

Requirements

As this package uses React's new Context API, React 16.3+ is required.

Run npx browserslist to see default, not ie 11 option in .browserslistrc compatibility. There is no IE 11 support starting in version 4.0.0.

Install

npm install --save @thrash-industries/react-actioncable-provider
# OR
yarn add @thrash-industries/react-actioncable-provider

Usage

The public API exports two components that you'll use: <ActionCableProvider /> and <ActionCableConsumer />.

<ActionCableProvider />

The provider is used in an outer container and wraps all of the components that may or may not consume the context. It accepts one of two props: url and cable. Passing url will result in the provider instantiating its own ActionCable.Consumer with that URL. Passing cable allows you to manually instantiate an ActionCable.Consumer on your own and pass it to the provider to be used by all descendent consumers.

With url

<ActionCableProvider url="ws://test.example.com/cable">...</ActionCableProvider>

With cable

import ActionCable from "actioncable";

const cable = ActionCable.createConsumer("ws://test.example.com/cable");

<ActionCableProvider cable={cable}>...</ActionCableProvider>;

<ActionCableConsumer />

The consumer will wrap an individual component. It accepts several props:

  • channel [String] Name of the channel to which you want to subscribe.
  • channel [Object] An object with a channel key which denotes the channel to which you want to subscribe. All other keys are passed to the channel as params.
  • onConnected [Function] A handler function that is called when the channel connects.
  • onDisconnected [Function] A handler function that is called when the channel disconnects.
  • onInitialized [Function] A handler function that is called when the ActionCable.Consumer is initialized.
  • onRejected [Function] A handler function that is called when the requested subscription is rejected.
  • onReceived [Function] A handler function that is called when the channel transmits a message to the client.
import React from "react";
import PropTypes from "prop-types";
import { ActionCableConsumer } from "@thrash-industries/react-actioncable-provider";

export default class Widget extends React.Component {
  static propTypes = {
    message: PropTypes.string
  };

  constructor(...props) {
    super(...props);

    this.handleReceived = this.handleReceived.bind(this);

    this.state = {
      message: ""
    };
  }

  handleReceived(message) {
    this.setState(state => {
      return {
        message
      };
    });
  }

  render() {
    return (
      <ActionCableConsumer
        channel="WidgetChannel"
        onReceived={this.handleReceived}
      >
        <h1>{this.state.message}</h1>
      </ActionCableConsumer>
    );
  }
}

Publishing

  • yarn Install dependencies
  • yarn test Run tests
  • yarn build Build for production
  • npm version [patch|minor|major] Increment version
  • npm publish Publish to npm

Other Uses

React Native

See https://github.com/cpunion/react-native-actioncable

Server Side Rendering

See cpunion#8

Example: https://github.com/cpunion/react-actioncable-ssr-example

react-actioncable-provider's People

Contributors

cpunion avatar dependabot[bot] avatar hashwin avatar horiaradu avatar jackhowa avatar marcelkooi avatar mgoggin avatar peterrkang avatar snyk-bot avatar whatcould avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

react-actioncable-provider's Issues

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.