Connecting To React
Last updated
Last updated
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 , and the . These concepts are important, and will help you build more scalable applications, so don't skip those links!
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:
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:
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: