> 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/examples/typeahead.md).

# Typeahead

## Typeahead

| callbag                                                                                                                             | most                                                                                                                             | RxJS                                                                                                                             | xstream                                                                                                                             |
| ----------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| [`code`](https://git.io/fAZ1E) [`live`](https://codesandbox.io/s/github/fanduel-oss/refract/tree/master/examples/typeahead/callbag) | [`code`](https://git.io/fAZ1W) [`live`](https://codesandbox.io/s/github/fanduel-oss/refract/tree/master/examples/typeahead/most) | [`code`](https://git.io/fAZ1n) [`live`](https://codesandbox.io/s/github/fanduel-oss/refract/tree/master/examples/typeahead/rxjs) | [`code`](https://git.io/fAZ1G) [`live`](https://codesandbox.io/s/github/fanduel-oss/refract/tree/master/examples/typeahead/xstream) |

In this example, a GitHub username searchbox is rendered to the screen. The searchbox includes a typeahead, which fetches username suggestions based on what the user has typed so far.

When a user clicks on a suggestion, or presses the return key while the textbox (or a suggestion) is focused, the selected GitHub account's data is fetched, and rendered to the screen.

### Aperture

Two sources are observed in this example:

* The `search` prop.
* The `selection` prop.

The `search` prop is used as the source for two separate streams.

First, a stream of suggestions. Each time the `search` prop changes, if its value is truthy, it is debounced and sent through a pipeline which includes an asynchronous fetch request, and some data manipulation.

In a separate stream of logic, each time the `search` prop changes, if its value is an empty string, it is mapped to another `setState` effect which resets the `suggestions` state to an empty array.

Finally, a stream of users. Each time the `selection` prop changes, it is sent through a similar logic pipeline to the first stream, which results in the `user` state being updated.

These three streams are merged into one, and returned as the output of the `aperture`.

Note that all three streams used a `toState` "effectCreator" function at the end - this is a helper function similar to a redux `actionCreator`.

### Handler

If the effect's type is `SET_STATE`, the `handler` passes the effect's payload into `initialProps.setState`.

### Result

The end result is an intelligent user interface which exposes search suggestions and fetches user data as a side-effect.


---

# 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/examples/typeahead.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.
