npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hz

v0.4.0

Published

React Horizon makes it easier to use your React application with horizon.io realtime backend

Downloads

24

Readme

npm Build Status

React Horizon

React Horizon makes it easier to use your React application with horizon.io realtime backend

Installation

$ npm i react-hz

React Horizon allows reactive dataflow between backend and React.js application. Client demand is declared in React components using Horizon's query API and data is synchronized thanks to horizon.io realtime backend.

Running example

  • Make sure you have installed RethinkDB and Horizon's CLI
  • Start server from example directory: $ hz serve --dev
  • Open http://127.0.0.1:8181 in your browser

Usage

Read Horizon's Collection API for querying methods.

react-hz package provides HorizonProvider instance provider component, HorizonRoute application route component, connector function and Horizon client library.

<HorizonProvider />

HorizonProvider is a top level component in your application which establishes connection to Horizon server. The component accepts an instance of Horizon constructor as instance prop.

<HorizonProvider instance={horizonInstance}>
  <App />
</HorizonProvider>

Horizon([config])

Horizon is a constructor function from Horizon's client library included into react-hz. Constructor function accepts optional config object http://horizon.io/api/horizon/#constructor.

const horizonInstance = Horizon({ host: 'localhost:8181' });

<HorizonRoute />

HorizonRoute is a top level component for every screen in your application which provides an API to respond to connectivity status changes. Normally you should render your app in renderSuccess callback. renderFailure callback receives error object which can be used to render an error message.

<HorizonRoute
  renderConnecting={() => <h1>Connecting...</h1>}
  renderDisconnected={() => <h1>You are offline</h1>}
  renderConnected={() => <h1>You are online</h1>}
  renderSuccess={() => <h1>Hello!</h1>}
  renderFailure={(error) => <h1>Something went wrong...</h1>} />

connect(component, config)

connect function wraps React components with specified queries for subscriptions and mutations. Connector function expects two arguments: React component and subscriptions/mutations config object. Props passed into container component are automatically passed into wrapped component.

const AppContainer = connect(App, {
  subscriptions: {
    // ...
  },
  mutations: {
    // ...
  }
});

withQueries(config)

withQueries is like connect, but designed to be used as a decorator. If you have enabled the decorator syntax in your project, instead of using connect like above, you can do the following:


import {withQueries} from 'react-hz'

@withQueries({
  subscriptions: {
    // ...
  },
  mutations: {
    // ...
  }
})
class MyComponent extends Component {
  // ...
}

Subscriptions

subscriptions is a map of subscription names to query functions. Data behind query is available as a prop with the same name in React component. Query function receives Horizon hz function which should be used to construct a query using Horizon's Collection API and props object which is being passed into container component.

Behind the scenes React Horizon calls watch and subscribe function on query object which returns RxJS Observable and subscribes to incoming data. Data received by that observable is then passed into React component as props.

All subscriptions are unsubscribed automatically on componentWillUnmount.

import React, { Component } from 'react';
import { render } from 'react-dom';
import { Horizon, HorizonProvider, connect } from 'react-hz';

class App extends Component {
  render() {

    const itemsSubcription = this.props.items;

    return (
      <ul>{itemsSubcription.map(({ id, title }) => <li key={id}>{title}</li>)}</ul>
    );
  }
}

const AppContainer = connect(App, {
  subscriptions: {
    items: (hz, { username }) => hz('items')
      .find({ username })
      .below({ id: 10 })
      .order('title', 'ascending')
  }
});

render((
  <HorizonProvider instance={Horizon()}>
    <AppContainer username='John' />
  </HorizonProvider>
), document.getElementById('app'));

Mutations

mutations is a map of mutation query names to mutation query functions. Specified mutations are available as props in React component behind their corresponding names in config.

Available mutation operations:

  • remove - http://horizon.io/api/collection/#remove
  • removeAll - http://horizon.io/api/collection/#removeall
  • replace - http://horizon.io/api/collection/#replace
  • store - http://horizon.io/api/collection/#store
  • upsert - http://horizon.io/api/collection/#upsert

It's possible to create two types of mutations (see example below):

  • generic mutation which provides mutation object and thus gives you an ability to call different mutation operations in component
  • specific mutation which is a function that receives parameters required for mutation, instantiates mutations object and applies mutation immediately
import React, { Component } from 'react';
import { render } from 'react-dom';
import { Horizon, HorizonProvider, connect } from 'react-hz';

class App extends Component {
  render() {

    const itemsMutation = this.props.items;
    const removeItem = this.props.removeItem;

    return (
      <div>
        <button onClick={() => itemsMutation.store({ title: 'Item' })}>add</button>
        <button onClick={() => removeItem(24)}>remove</button>
      </div>
    );
  }
}

const AppContainer = connectHorizon(App, {
  mutations: {
    items: (hz) => hz('items'),
    removeItem: (hz) => (id) => hz('items').remove(id)
  }
});

render((
  <HorizonProvider instance={Horizon()}>
    <AppContainer />
  </HorizonProvider>
), document.getElementById('app'));

Limitations

MIT