1.2.0 • Published 2 years ago

@yext/answers-headless-react v1.2.0

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

Answers Headless React

Answers Headless React is the official React UI Bindings layer for Answers Headless.

Written in 100% TypeScript.

Installation

npm install @yext/answers-headless-react

Getting Started - AnswersHeadlessProvider

Answers Headless React includes an <AnswersHeadlessProvider /> component, which instantiates an AnswersHeadless instance and makes it available to the rest of your app.

import { AnswersHeadlessProvider } from '@yext/answers-headless-react';
import SearchBar from './SearchBar';
import MostRecentSearch from './MostRecentSearch';
import UniversalResults from './UniversalResults';

function MyApp() {
  return (
    <AnswersHeadlessProvider
      apiKey='your api key'
      experienceKey='your experience key'
      locale='en'
    >
      {/* Add components that use Answers as children */}
      <SearchBar/>
      <MostRecentSearch/>
      <UniversalResults/>
    </AnswersHeadlessProvider>
  );
}

Respond to State Updates with useAnswersState

useAnswersState reads a value from the AnswersHeadless state and subscribes to updates.

import { useAnswersState } from '@yext/answers-headless-react';

export default function MostRecentSearch() {
  const mostRecentSearch = useAnswersState(state => state.query.mostRecentSearch);
  return <div>Showing results for {mostRecentSearch}</div>;
}

Dispatch Actions with useAnswersActions

useAnswersActions allows you to dispatch actions using the AnswersHeadless instance.

These include performing searches, getting autocomplete suggestions, and adding filters.

For a full list of capabilities see the answers-headless docs.

import { useAnswersActions } from '@yext/answers-headless-react';
import { ChangeEvent, KeyboardEvent, useCallback } from 'react';

function SearchBar() {
  const answers = useAnswersActions();
  const handleTyping = useCallback((e: ChangeEvent<HTMLInputElement>) => {
    answers.setQuery(e.target.value);
  }, [answers]);
  
  const handleKeyDown = useCallback((evt: KeyboardEvent<HTMLInputElement>) => {
    if (evt.key === 'Enter' ) {
      answers.executeUniversalQuery();
    }
  }, [answers]);

  return <input onChange={handleTyping} onKeyDown={handleKeyDown}/>;
}

Class Components

For users that want to use class components instead of functional components, you can use the AnswersHeadlessContext directly to dispatch actions, and the subscribeToStateUpdates HOC to receive updates from state.

These also work with functional components.

subscribeToStateUpdates

Here is our MostRecentSearch component again, rewritten as a class with subscribeToStateUpdates.

import { subscribeToStateUpdates } from '@yext/answers-headless-react';
import { Component } from 'react';

interface Props {
  mostRecentSearch: string
}

class MostRecentSearch extends Component<Props> {
  render() {
    return <div>Showing results for {this.props.mostRecentSearch}</div>;
  }
}

export default subscribeToStateUpdates(state => ({
  mostRecentSearch: state.query.mostRecentSearch
}))(MostRecentSearch);

AnswersHeadlessContext

And here is our simple SearchBar again, rewritten as a class using AnswersHeadlessContext.

import { AnswersHeadlessContext, AnswersHeadless } from '@yext/answers-headless-react';
import { Component } from 'react';

export default class Searcher extends Component {
  static contextType = AnswersHeadlessContext;

  render() {
    const answers: AnswersHeadless = this.context;
    return <input
      onChange={evt => answers.setQuery(evt.target.value)}
      onKeyDown={evt => {
        if (evt.key === 'Enter') {
          answers.executeUniversalQuery();
        }
      }}
    />
  }
}

useAnswersUtilities

We offer a useAnswersUtilities convenience hook for accessing AnswersHeadless.utilities, which offers a number of stateless utility methods. The answersUtilities and answersUtilitiesFromActions variables below are equivalent.

For class components, you can access AnswersUtilities through AnswersHeadlessContext.

const answersUtilities = useAnswersUtilities();
const answersUtilitiesFromActions = useAnswersActions().utilities;
1.2.0

2 years ago

1.1.1-alpha-122

2 years ago

1.1.0-beta.10

2 years ago

1.1.0-beta.11

2 years ago

1.1.0-beta.12

2 years ago

1.1.0-beta.13

2 years ago

1.1.1-beta.128

2 years ago

1.1.1-alpha.125

2 years ago

1.1.1-alpha.124

2 years ago

1.1.1-alpha.123

2 years ago

1.1.0-beta.9

2 years ago

1.1.0-beta.8

2 years ago

1.1.1-alpha.1

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.0-alpha.7

2 years ago

1.1.0-alpha.0

2 years ago

1.1.0-alpha.6

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.4.1-beta.1

3 years ago

0.4.1-beta.0

3 years ago

0.4.2-beta.0

2 years ago

0.4.0-beta.0

3 years ago

0.4.0-beta.1

3 years ago

0.10.0

2 years ago

1.1.0-beta.7

2 years ago

1.1.0-beta.2

2 years ago

1.1.0-beta.1

2 years ago

1.1.0-beta.0

2 years ago

1.1.0-beta.6

2 years ago

1.1.0-beta.5

2 years ago

1.1.0-beta.4

2 years ago

1.1.0-beta.3

2 years ago

0.9.0

2 years ago

0.9.1

2 years ago

0.4.3

2 years ago

0.9.2-beta.0

2 years ago

0.3.0-beta.0

3 years ago

0.2.0-beta.7

3 years ago

0.2.0

3 years ago

0.2.0-beta.6

3 years ago

0.2.0-beta.5

3 years ago

0.2.0-beta.4

3 years ago

0.2.0-beta.3

3 years ago

0.2.0-beta.2

3 years ago

0.2.0-beta.1

3 years ago

0.1.0-beta.1

3 years ago

0.1.0-beta.0

3 years ago

0.2.0-beta.0

3 years ago

0.1.0

3 years ago