Refract
  • Read Me
  • Introduction
    • Why Refract?
    • Core Concepts
    • Thinking In Refract
    • Alternatives
  • Usage
    • Getting Started
    • Installation
    • Connecting To React
    • Observing React, Preact and Inferno
    • Injecting Dependencies
    • Observing Redux
    • Observing Anything
    • Handling Effects
    • Pushing to Props
    • Rendering Components
    • Handling Errors
    • Testing
  • Recipes
    • Dependency Injection
    • Creating An API Dependency
    • Handling state
    • Replacing Redux Connect
  • Examples
    • Debounced fetch
    • Counter
    • Field validation
    • Redux fetch
    • Routing
    • Typeahead
    • Visit time
  • API
    • compose
    • withEffects
    • toProps, asProps
    • useRefract
    • refractEnhancer (Redux)
  • Glossary
  • Feedback
Powered by GitBook
On this page
  • Background
  • Where Refract Belongs
  1. Usage

Connecting To React

PreviousInstallationNextObserving React, Preact and Inferno

Last updated 5 years ago

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

Background

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 , and the . 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 (
            <CounterWithEffects
                count={this.state.count}
                increment={this.increment}
            />
        )
    }
}
Dan Abramov's article explaining presentational/container components
React documentation page explaining HoCs