Connecting To React

Last updated 4 months ago

This document focuses on React, but the same applies to Inferno and Preact


Refract builds upon two ideas which have been embraced by the React community: separating presentational and container components, plus higher-order components (HoCs).

If you're unfamiliar with these concepts, the best places to start are Dan Abramov's article explaining presentational/container components, and the React documentation page explaining HoCs. These concepts are important, and will help you build more scalable applications, so don't skip those links!

Where Refract Belongs

The key insight behind presentational/container components is that your application's state and its view should be separated. In React, your state is typically passed into your view as props.

Refract sits between your state and your view, allowing you to observe the changes to those props over time. To achieve this, you simply wrap your view with Refract's withEffects higher-order component.

For example, given a simple view component:

const Counter = ({ count, increment }) => (
<button onClick={increment}>Count: {count}</button>

If we want to cause side-effects in response to changes in the props being passed into our Counter, we use withEffects to create an enhanced version of the component:

import { withEffects } from 'refract-rxjs'
// Note that the handler and aperture are explained later in the docs,
// these empty functions are just placeholders
const aperture = (component, initialProps) => {}
const handler = initialProps => effect => {}
const CounterWithEffects = withEffects(aperture), { handler }(Counter)

This new CounterWithEffects component now includes the side-effect logic included in our handler and aperture, and renders the original Counter presentational component unaltered. It can be used just like any other component:

class Container extends Component {
state = { count: 0 }
increment = () => this.setState(({ count }) => ({ count: count + 1 }))
render() {
return (