React Horizon makes it easier to use your React application with horizon.io realtime backend
React Horizon makes it easier to use your React application with horizon.io realtime backend
$ 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.
exampledirectory:
$ hz serve --dev
Read Horizon's Collection API for querying methods.
react-hzpackage provides
HorizonProviderinstance provider component,
HorizonRouteapplication route component, connector function and
Horizonclient library.
HorizonProvideris a top level component in your application which establishes connection to Horizon server. The component accepts an instance of
Horizonconstructor as
instanceprop.
js
Horizon([config])
Horizonis a constructor function from Horizon's client library included into
react-hz. Constructor function accepts optional config object http://horizon.io/api/horizon/#constructor.
js const horizonInstance = Horizon({ host: 'localhost:8181' });
HorizonRouteis 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
renderSuccesscallback.
renderFailurecallback receives error object which can be used to render an error message.
jsConnecting...
} renderDisconnected={() =>You are offline
} renderConnected={() =>You are online
} renderSuccess={() =>Hello!
} renderFailure={(error) =>Something went wrong...
} />
connect(component, config)
connectfunction 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.
js const AppContainer = connect(App, { subscriptions: { // ... }, mutations: { // ... } });
withQueries(config)
withQueriesis like
connect, but designed to be used as a decorator. If you have enabled the decorator syntax in your project, instead of using
connectlike above, you can do the following: ```js
import {withQueries} from 'react-hz'
@withQueries({ subscriptions: { // ... }, mutations: { // ... } }) class MyComponent extends Component { // ... } ```
subscriptionsis 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
hzfunction 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
watchand
subscribefunction 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 ( </pre><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(( ), document.getElementById('app'));
Mutations
mutationsis 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/#upsertIt'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(( ), document.getElementById('app'));
Limitations
MIT