> For the complete documentation index, see [llms.txt](https://refract.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://refract.js.org/usage/connecting-to-react.md).

# Connecting To React

> 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 [Dan Abramov's article explaining presentational/container components](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0), and the [React documentation page explaining HoCs](https://reactjs.org/docs/higher-order-components.html). 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:

```javascript
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:

```javascript
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:

```javascript
class Container extends Component {
    state = { count: 0 }

    increment = () => this.setState(({ count }) => ({ count: count + 1 }))

    render() {
        return (
            <CounterWithEffects
                count={this.state.count}
                increment={this.increment}
            />
        )
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://refract.js.org/usage/connecting-to-react.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
