Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-hz-pr8
Advanced tools
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.
example
directory: $ hz serve --dev
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: {
// ...
}
});
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
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/#removeremoveAll
- http://horizon.io/api/collection/#removeallreplace
- http://horizon.io/api/collection/#replacestore
- http://horizon.io/api/collection/#storeupsert
- http://horizon.io/api/collection/#upsertIt's possible to create two types of mutations (see example below):
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'));
MIT
FAQs
React Horizon makes it easier to use your React application with horizon.io realtime backend
The npm package react-hz-pr8 receives a total of 1 weekly downloads. As such, react-hz-pr8 popularity was classified as not popular.
We found that react-hz-pr8 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.