npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-orama

v0.1.7

Published

## React hooks for using Orama search

Downloads

1,223

Readme

use-orama

React hooks for using Orama search

🚧 TODO: publish to npm, then update examples deps so they can used in codesandbox

Quickstart

⏬ Install with your favorite package manager

# npm
npm install use-orama

# pnpm
pnpm add use-orama

# yarn
yarn add use-orama

📦 Wrap your components with the <OramaProvider> and define the schema

import {OramaProvider} from 'use-orama';

const schema = {title: 'string', description: 'string'};

// ...
export function AppWithSearch() {
  return (
    <OramaProvider schema={schema}>
      <App />
    <OramaProvider>
  )
}

🔍 Do some searching!

import {useSearchableData, useSearch} from 'use-orama';

const data = [
  {title: 'Gostbusters', description: 'Best movie ever made'},
  {title: 'Gooneys', description: 'Best movie of all time'},
];

const searchParameters = {term: 'ever'};

function App() {
  useSearchableData(data);
  const {done, results} = useSearch(searchParameters);

  if (!done) {
    return (<>Still searching</>);
  }

  return (
    <>
      {results.hits.map(
        hit => <div key={hit.id}>{hit.document.title}: {hit.document.descriptions}</div>
      )}
    </>
  );
}

It also integrates nicely with async fetching of the searchable data

import {useSetSearchableData, useSearch} from 'use-orama';
const searchParameters = {term: 'ever'};

function App() {
  const setSearchableData = useSetSearchableData();

  useEffect(() => {
    async function fetchData() {
      const result = await fetch('https://your.api/data');
      setSearchableData(await result.json());
    }
    fetchData();
  }, [])

  if (!done) {
    return (<>Still searching</>);
  }

  return (
    <>
      {results.hits.map(
        hit => <div key={hit.id}>{hit.document.title}: {hit.document.descriptions}</div>
      )}
    </>
  );
}

⚠️ Notes:

  • useSearchableData and useSearch do not need to be in the same component, but they do need to have a common <OramaProvider> ancestor.
  • To avoid excessive rerenders, the Orama options, schema, searchable data, and search parameters should either be declared in a scope exterior to the components or use react's useMemo or useState to avoid creating new objects on every render.

API

<OramaProvider>

Provider component that provides a common context for interfacing with Orama. By making an <OramaProvider> a common ancestor, components that set the searchable data and perform the search are decoupled.

Properties

| Property | Description | Default | |-|-|-| | schema | Orama schema | No default, this must be defined. Refer to Orama create. | | language | optional parameter passed to Orama create | undefined, lets Orama set the default. Refer to Orama stemming | | components | optional parameter passed to Orama create | undefined, lets Orama set the default. Refer to Orama components |

useSearchableData(data: any[]): void

Sets the data to be indexed by Orama as a parameter to the hook. Useful in cases when the data is constant. useSearchableData and useSetSearchableData are mutually exclusive, use one or the other.

Parameters

| Parameter | Description | |-|-| | data | The data to index with orama |

useSetSearchableData(): (data: any[]) => void

Returns a callback function that can be used to set the data to be indexed Orama. Useful in cases when the data is retrieved asynchronously via an API or database. useSearchableData and useSetSearchableData are mutually exclusive, use one or the other.

Parameters

| Parameter | Description | |-|-| | return | setter callback that can be called with the data to be indexed with orama |

useSearch(parameters: SearchParams): {done: boolean; results: SearchResult | undefined; }

Sets the search parameters for Orama and returns the search results. A convenience boolean, done is also returned to easily determine when searching is done.

Parameters

| Parameter | Description | Default | |-|-|-| | parameters | search parameters passed to Orama search | No default, this must be defined. Refer to Orama search. | | return | {done, results} An object with a boolean done that will be true when indexing and searching is done and results that contains the Orama search results (will be undefined when done is false) | |