Search #2: Live search results #8
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Instant Search Results
This builds off #7 .
Steps
CategoriesController, you'll see that, similar to Add deferred props #3 , we have simulated a slow computing method that renders prop data.render inertiamethod, comment outsomething_expensiveand uncomment the line above it.Esckey, the search form will unfocus, closing the instant results.What is happening?
Shared data
Because we want to make our search results available on multiple pages, i.e. any page that the search bar is rendered on, we want to use shared data. We've added our
inertia_sharecall to a controller concern to make it easier to include it on multiple pages.Partial Reloads
The
<input />element now has a ReactonChangehandler with a debounced search call. This search call executes a partial reload, which makes an Inertia request to the current url, and tells our renderer to filter out everything except thesearch_resultsprop.There is an important nuance to this, though. In
CategoriesController, passingsomething_expensiveto the Inertia renderer immediately runs that method. So, the renderer filters it out from the response after it has executed. The result is that our partial reload is not actually improving performance. The fix is simple: wrap the method call is something callable [note that all of the InertiaRails lazy evaluation options are callables]. It's good practice to wrap props that you know will be slow to compute in a callable, just in case someone in the future adds a partial reload for that page.Escape key behavior
The escape key functionality has absolutely nothing to do with Inertia! It's just React code with a ref and and a key down handler. A key benefit of Inertia is removing or avoiding a lot of React hook calls. But you probably don't want zero hook calls. This is a good example of where you might want one: to improve an experience that lives in the UI only. No server communication is required in this interaction, so Inertia doesn't need to be involved.