React hook to search?

I don’t want to use the React UI Widgets. I’ve created my own.

What I’d like to do is use a React hook I can use to call to get the results and send it to my React components. Is there an Algolia React Hook to do this?

Hi there!

We don’t expose any hook as of right now in our libraries but it is pretty simple to create one. I actually did that a couple times. In that case, you want to use algoliasearch directly.

Then, what it boils down to is using an async function in a react component, so you’ll find lots of libraries that gives you hooks to do so (for example, react-async gives you a useAsync hook).

It’ll look very similar to this:

const search = useAsync(() =>
  client.initIndex(index).search(query, options)
)

return search.status === 'pending'
  ? <p>Loading...<p>
  : search.status === 'resolved'
  ? <MyCustomList item={search.data.hits} />
  : search.status === 'error'
  ? <p>Error :(</p>
  : null

Does that make sense?