Adding search to a React/Next.js blog
Published: 2023-07-02 12:32:36

---
title: Adding search to a React/Next.js blog
published: true
date: 2023-07-02 11:15:09 UTC
tags: 
canonical_url: https://felixrunquist.com/posts/adding-search-to-a-react-blog
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8og7dsqw96oygvbd7tg.png

---

When I [built my Next.js-based](https://felixrunquist.com/posts/how-i-built-my-website) website, one of the things I really wanted to implement was the ability to search through posts. I recently came across Kent C. Dodd’s `match-sorter` package, a Node.js package which allows for speedy filtering through text arrays. It immediately made me think of its usage as a search algorithm.

Here’s the idea: On the client, we can provide the page with an array of post metadata: titles, excerpts and slugs. We add an event listener to the search input which runs `matchSorter` on the array with the content of the search input, and we display the resulting posts.

>I had a look at <a href="https://github.com/kentcdodds/kentcdodds.com">Kent C. Dodd’s website</a> and it turns out that he uses match-sorter as well in his search algorithm!

The basic usage of `match-sorter` is really simple: You provide it an array and a string to match with, and it returns an ordered array based on what ranks the highest. This makes for a really simple implementation below, with react functions to manage state:

```
import { matchSorter, rankings } from ‘match-sorter’
import {useState, useEffect} from ‘react’
const search = [‘Hi’, ‘Hello’, ‘Hi there’]
export default function App(){
  const [input, setInput] = useState(‘\’)
  const [results, setResults] = useState([])
  useEffect(() =&gt; {
    var res = matchSorter(search, input)
    setResults(res)
  }, [input])
  return (
    &lt;&gt;
      <input type="text" value="{input}" oninput="{e"> setInput(e.target.value)}/&gt;
      {results.map(i =&gt; <p key="{i}">{i}</p>)}
    &gt;
  )
}
```

However, in the case of an article search system, we want to do more: we’d like to be able to search not only through an array of titles, but through other information, such as a short excerpt for example. This data will be fetched from the CMS and provided to the browser, resulting in an object like so:

```
const posts = [
  {title: ‘How I built my website’, excerpt: ‘Trying to figure out how to build my website was a challenge for me. As a web designer, I wanted it to be fully customisable, but I also wanted ease of use to reduce friction and make it easy to maintain.’, url: ‘https://felixrunquist.com/posts/how-i-built-my-website’},
  {title: ‘Implementing online payments with Stripe’, excerpt: ‘A tutorial on setting up a credit card payment form on your NextJS site. We will cover signing up and the Stripe SDK’, url: ‘https://felixrunquist.com/posts/online-payments-stripe’},
  {title: ‘Next.js HTTP Authentication with JWT and cookies’, excerpt: ‘I was recently trying to figure out a way to implement simple HTTP authentication for a personal Next.js project. I augmented the basic HTTP auth with JWT and cookies’, url: ‘https://felixrunquist.com/posts/next-js-http-authentication-jwt-cookies’},
  {title: ‘Creating and ordering a custom PCB’, excerpt: ‘As part of a home automation project, I wanted to house a thermometer, motion sensor and display in a small form factor. I decided to design and order my own PCB.’, url: ‘https://felixrunquist.com/posts/creating-and-ordering-custom-pcb’}
]
```

The smart thing about the `matchSorter` function is that it can sort through arrays of objects as well: You can tell which key you’d like it to sort through, and you can even provide specific criteria for each key: for example, you can choose to match exact case, or complete equality.

Let’s see how one could implement this:

```
const posts = [
  {title: ‘How I built my website’, excerpt: ‘Trying to figure out how to build my website was a challenge for me. As a web designer, I wanted it to be fully customisable, but I also wanted ease of use to reduce friction and make it easy to maintain.’, url: ‘https://felixrunquist.com/posts/how-i-built-my-website’},
  {title: ‘Implementing online payments with Stripe’, excerpt: ‘A tutorial on setting up a credit card payment form on your NextJS site. We will cover signing up and the Stripe SDK’, url: ‘https://felixrunquist.com/posts/online-payments-stripe’},
  {title: ‘Next.js HTTP Authentication with JWT and cookies’, excerpt: ‘I was recently trying to figure out a way to implement simple HTTP authentication for a personal Next.js project. I augmented the basic HTTP auth with JWT and cookies’, url: ‘https://felixrunquist.com/posts/next-js-http-authentication-jwt-cookies’},
  {title: ‘Creating and ordering a custom PCB’, excerpt: ‘As part of a home automation project, I wanted to house a thermometer, motion sensor and display in a small form factor. I decided to design and order my own PCB.’, url: ‘https://felixrunquist.com/posts/creating-and-ordering-custom-pcb’}
]//Retrieved by the CMS

import { matchSorter, rankings } from ‘match-sorter’
import {useState, useEffect} from ‘react’
export default function App(){
  const [input, setInput] = useState(‘\’)
  const [results, setResults] = useState([])
  useEffect(() =&gt; {
    var res = matchSorter(posts, input, {keys: [{key: ‘title’}, {key: ‘excerpt’}]})
    setResults(res)
  }, [input])
  return (
    &lt;&gt;
      <input type="text" value="{input}" oninput="{e"> setInput(e.target.value)}/&gt;
      {results.map(i =&gt; <a href="{i.url}" key="{i.title}"><h2>{i.title}</h2>
<p>{i.excerpt}</p></a>)}
    &gt;
  )
}
```
>As the array to search through is being passed to the client, you probably don’t want to provide too large objects which could add to loading times, and cause performance issues. I’d limit myself to one thousand items!

We’ve now implemented a basic search function, that has an input, a function to display results, and a function that updates results when the input is written to. Let’s say the search page is at `www.example.com/search`. What if one was to reload the page, or if one wanted to share the url to the exact results? One could store the current search state in the URL, with the query parameter. For instance, searching “Recipes” would dynamically update the url so that it points at `www.example.com/search?q=Recipes`. Like that, refreshing or sharing the search results would bring you back to the exact same state.

This can be done in React using `react-router-dom` (it can also be done in Next.js using `useRouter`), a library that manages URLs and paths. First you define a retrieval function, checking on page load whether a query string has been provided or not. When the text input is written to, you can dynamically update the query parameter.

```
const posts = [
{title: ‘How I built my website’, excerpt: ‘Trying to figure out how to build my website was a challenge for me. As a web designer, I wanted it to be fully customisable, but I also wanted ease of use to reduce friction and make it easy to maintain.’, url: ‘https://felixrunquist.com/posts/how-i-built-my-website’},
{title: ‘Implementing online payments with Stripe’, excerpt: ‘A tutorial on setting up a credit card payment form on your NextJS site. We will cover signing up and the Stripe SDK’, url: ‘https://felixrunquist.com/posts/online-payments-stripe’},
{title: ‘Next.js HTTP Authentication with JWT and cookies’, excerpt: ‘I was recently trying to figure out a way to implement simple HTTP authentication for a personal Next.js project. I augmented the basic HTTP auth with JWT and cookies’, url: ‘https://felixrunquist.com/posts/next-js-http-authentication-jwt-cookies’},
{title: ‘Creating and ordering a custom PCB’, excerpt: ‘As part of a home automation project, I wanted to house a thermometer, motion sensor and display in a small form factor. I decided to design and order my own PCB.’, url: ‘https://felixrunquist.com/posts/creating-and-ordering-custom-pcb’}
]//Retrieved by the CMS

import { matchSorter, rankings } from ‘match-sorter’
import {useState, useEffect} from ‘react’
import { useSearchParams, BrowserRouter as Router} from “react-router-dom”;
export default function App(){
  return (
    <router>
      <search></search>
    </router>
  )
}

function Search(){
  const [searchParams, setSearchParams] = useSearchParams();
  const [input, setInput] = useState(‘\’)
  const [results, setResults] = useState([])
  useEffect(() =&gt; {
    setInput(searchParams.get(‘search’))//Get search parameters on load
  },[])

  useEffect(() =&gt; {
    var res = matchSorter(posts, input, {keys: [{key: ‘title’}, {key: ‘excerpt’}]})
    setResults(res)
    if(input){//Update query parameters
      setSearchParams({ ‘search’: input });
    }
  }, [input])
  return (
    &lt;&gt;
      <input type="text" value="{input}" oninput="{e"> setInput(e.target.value)}/&gt;
      {results.map(i =&gt; <a href="{i.url}" key="{i.title}"><h2>{i.title}</h2>
<p>{i.excerpt}</p></a>)}
    &gt;
  )
}
```

That’s all there is to it! Luckily, the `react-sorter` package does most of the heavy lifting, we just did the content fetching and state implementation. Let me know if this was helpful by [reaching out to me on Twitter](https://twitter.com/intent/tweet?via=felixrunquist)!

External links:

- Match-sorter documentation: [https://github.com/kentcdodds/match-sorter](https://github.com/kentcdodds/match-sorter)
- URL-based state: [https://blog.logrocket.com/use-state-url-persist-state-usesearchparams/](https://blog.logrocket.com/use-state-url-persist-state-usesearchparams/)

This article was retrieved from [felixrunquist.com](felixrunquist.com)