algolia / react-instantsearch

⚡️ Lightning-fast search for React and React Native applications, by Algolia.
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/react/
MIT License
1.97k stars 386 forks source link

feat(hooks): introduce `useInstantSearch()` #3494

Closed Haroenv closed 2 years ago

Haroenv commented 2 years ago

Summary

useInstantSearch is a new hook to access advanced functionality of InstantSearch without creating custom widgets:

Result

codesandbox-ci[bot] commented 2 years ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit af4488fc549bcfbfd692eed36bbd9b79c6d5f2dc:

Sandbox Source
algolia/create-instantsearch-app Configuration
routing-basic Configuration
hooks-example Configuration
netlify[bot] commented 2 years ago

Deploy Preview for react-instantsearch ready!

Name Link
Latest commit fa06a575212e1b297347d420e094f85a5613fe1e
Latest deploy log https://app.netlify.com/sites/react-instantsearch/deploys/62973c0d355415000944d086
Deploy Preview https://deploy-preview-3494--react-instantsearch.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

netlify[bot] commented 2 years ago

Deploy Preview for react-instantsearch ready!

Name Link
Latest commit af4488fc549bcfbfd692eed36bbd9b79c6d5f2dc
Latest deploy log https://app.netlify.com/sites/react-instantsearch/deploys/62a06b27056be90008170e7e
Deploy Preview https://deploy-preview-3494--react-instantsearch.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

francoischalifour commented 2 years ago

What I wanted to put the light on in https://github.com/algolia/react-instantsearch/pull/3494#discussion_r886660022 is that it's not possible to recreate a router from the use() API because the middleware seems to be added after the first render.

You can see this in the <SearchRouter> component in this sandbox. Click a refinement, refresh, notice that the refinement disappears.

RobbyUitbeijerse commented 2 years ago

What I wanted to put the light on in https://github.com/algolia/react-instantsearch/pull/3494#discussion_r886660022 is that it's not possible to recreate a router from the use() API because the middleware seems to be added after the first render.

@Haroenv might be interesting to provide (initial) middleware like so?

const {} = useInstantSearch({ 
  use: [middleware1, middleware2]
})

similar to how it's been implemented in swr: https://swr.vercel.app/docs/middleware

Haroenv commented 2 years ago

Sounds interesting, but I wonder if those also would be added too late. Maybe it should be a prop to the InstantSearch component instead of a use function?

francoischalifour commented 2 years ago

@RobbyUitbeijerse @Haroenv Thinking back at it, it makes sense that we cannot recreate a complete router from use(), and this is because InstantSearch.js treats routing as a special middleware internally, which subscribes before start(). So this is not a blocker for useInstantSearch().use. The routing prop would be the way to plug a routing system.

A more conventional middleware that I tried to reimplement is the Insights middleware with <InsightsMiddleware> in this sandbox. You can see that from the useInstantSearch().use standpoint, it works correctly.

So to me this is enough to validate this API.

francoischalifour commented 2 years ago

For reference, I also created a <ScrollTo> component based on use(). It's similar to the official <ScrollTo> widget from v6.