Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Commit 6924c27

Browse files
Merge pull request #3 from mikefrancis/debounced-search
Debounce search input
2 parents 60fd672 + 3d67e61 commit 6924c27

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"gatsby-source-filesystem": "^2.0.32",
2727
"gatsby-transformer-remark": "^2.3.12",
2828
"gatsby-transformer-sharp": "^2.1.18",
29+
"lodash.debounce": "^4.0.8",
2930
"prismjs": "^1.16.0",
3031
"react": "^16.8.6",
3132
"react-dom": "^16.8.6",

src/pages/search.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState, useEffect } from "react"
2+
import debounce from "lodash.debounce"
23
import Layout from "../components/layout"
34
import SearchForm from "../components/searchForm"
45
import SearchResults from "../components/searchResults"
@@ -9,12 +10,17 @@ const Search = ({ data, location }) => {
910

1011
useEffect(() => {
1112
if (searchQuery && window.__LUNR__) {
12-
window.__LUNR__.__loaded.then(lunr => {
13+
const debouncedSearch = debounce(async () => {
14+
const lunr = await window.__LUNR__.__loaded
1315
const refs = lunr.en.index.search(searchQuery)
1416
const posts = refs.map(({ ref }) => lunr.en.store[ref])
17+
1518
setResults(posts)
16-
})
19+
}, 500)
20+
21+
debouncedSearch()
1722
}
23+
1824
if (!searchQuery) setResults([])
1925
}, [location.search])
2026

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6945,6 +6945,11 @@ lodash.clonedeep@^4.5.0:
69456945
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
69466946
integrity sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=
69476947

6948+
lodash.debounce@^4.0.8:
6949+
version "4.0.8"
6950+
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
6951+
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=
6952+
69486953
lodash.escaperegexp@^4.1.2:
69496954
version "4.1.2"
69506955
resolved "https://registry.yarnpkg.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347"

0 commit comments

Comments
 (0)