Navigation Menu

Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Commit

Permalink
Step 3: creating a dynamic search with user input
Browse files Browse the repository at this point in the history
  • Loading branch information
colbyfayock committed May 24, 2020
1 parent adbf30a commit 1b8918f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/App.js
@@ -1,10 +1,12 @@
import React from 'react';
import React, { useState } from 'react';
import './App.css';
import Fuse from 'fuse.js';

import characters from './characters.json';

function App() {
const [query, updateQuery] = useState('');

const fuse = new Fuse(characters, {
keys: [
'name',
Expand All @@ -14,8 +16,12 @@ function App() {
includeScore: true
});

const results = fuse.search('bender');
const characterResults = results.map(character => character.item);
const results = fuse.search(query);
const characterResults = query ? results.map(character => character.item) : characters;

function onSearch({ currentTarget }) {
updateQuery(currentTarget.value);
}

return (
<>
Expand Down Expand Up @@ -53,7 +59,7 @@ function App() {
<aside>
<form className="search">
<label>Search</label>
<input type="text" />
<input type="text" value={query} onChange={onSearch} />
</form>
</aside>
</main>
Expand Down

2 comments on commit 1b8918f

@chuckis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
Interesting... Just trying dynamic search and got this.

@colbyfayock
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah! so a tutorial is coming along with this :) i will be posting Tuesday.

but fuse.js is "fuzzy" search, meaning it will try to predict what your search actually is looking for, not necessarily an exact match search. this helps for things like misspellings or if you're searching and you don't know exactly what the match is

you can check out some of the options for tuning that here: https://fusejs.io/api/options.html#fuzzy-matching-options

Please sign in to comment.