# DOM references

To interact with actual DOM Element — you need to acquire a reference to it. To get a reference, you can create a Subject and pass that subject to special ref property on JSX element:

```jsx
import Recks from 'recks';
import { Subject } from 'rxjs';

function App() {
  const ref$ = new Subject();

  ref$.subscribe(ref => {
    ref.style.background = 'magenta';
  });

  return <div ref={ref$}>Hello!</div>;
}
```

[online sandbox](https://codesandbox.io/s/recks-basic-ref-mvysf?file=/src/App.jsx)

If DOM tree is updated **`ref$`** will emit a reference to the new DOM element.

{% hint style="info" %}
**`NOTE: ref$`** will automatically complete with the component
{% endhint %}

### Focus example

Here's a more sophisticated example with a Button, focusing Input on click:

```jsx
import Recks from 'recks';
import { Subject } from 'rxjs';
import { withLatestFrom, takeUntil } from 'rxjs/operators';

function TextInputWithFocusButton(props$, { destroy$ }) {
  const ref$    = new Subject();
  const clicks$ = new Subject();

  clicks$
    .pipe(
      withLatestFrom(ref$, (_, ref) => ref),
      takeUntil(destroy$)
    )
    .subscribe(ref => {
      ref.focus();
    });

  return (
    <div>
      <input  ref={ref$} type="text" />
      <button onClick={ ()=>clicks$.next(null) }>
        Focus the input
      </button>
    </div>
  );
}
```

[online sandbox](https://codesandbox.io/s/recks-example-input-ref-ye5so?fontsize=14\&hidenavigation=1\&theme=dark\&module=/src/App)


---

# Agent Instructions: 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:

```
GET https://recks.gitbook.io/recks/api/dom-references.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
