GithubHelp home page GithubHelp logo

isabella232 / answers-headless-react Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yext/search-headless-react

0.0 0.0 0.0 3.7 MB

React UI bindings for the @yext/answers-headless library

JavaScript 2.22% TypeScript 97.78%

answers-headless-react's Introduction

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;

answers-headless-react's People

Contributors

bhainesva avatar cea2aj avatar cjiang2000 avatar nmanu1 avatar oshi97 avatar tmeyer2115 avatar yen-tt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.