@cajuncodemonkey/naics-search-react
v1.0.0
Published
React hook wrapping @cajuncodemonkey/naics-search's async load-state.
Readme
@cajuncodemonkey/naics-search-react
A thin React hook wrapping @cajuncodemonkey/naics-search's
async load-state, so a React app doesn't have to hand-roll "is the model loaded yet,
did it error" boilerplate around loadNaics().
Built for naics-code-resolver —
see that repo for the full resolver UI this package's sibling naics-search powers.
Install
npm install @cajuncodemonkey/naics-search-react @cajuncodemonkey/naics-searchreact (^19) is a peer dependency — bring your own, this package doesn't bundle it.
Usage
import { useNaicsSearch } from "@cajuncodemonkey/naics-search-react";
function Resolver() {
const naics = useNaicsSearch();
if (naics.status === "loading") return <p>Loading…</p>;
if (naics.status === "error") return <p>Couldn't load NAICS data: {String(naics.error)}</p>;
// naics.status === "ready" — search()/drilldown fns are available
return <SearchForm onSubmit={(text) => naics.search(text)} />;
}API
useNaicsSearch(): Status
No arguments — data-source configuration (if you need it) is set globally via
naics-search's configureDataProvider() before your app mounts, not through this
hook. Status is a discriminated union on status:
type Status =
| { status: "loading" }
| { status: "error"; error: unknown }
| {
status: "ready";
search: typeof search;
drilldownOptions: typeof drilldownOptions;
getNode: typeof getNode;
isResolved: typeof isResolved;
getAncestorPath: typeof getAncestorPath;
censusUrl: typeof censusUrl;
};TypeScript narrows naics.search/the drilldown fns into scope only once
naics.status === "ready" — there's no runtime guard to remember, the type system
enforces it.
The hook owns no app policy — confidence bands, settings, Q&A UI flow all stay in your app, same as they do in naics-code-resolver's own app.
Caveats
- Unmounting mid-load is safe — the hook guards against
setStateon an unmounted component. It does not cancel the underlyingloadNaics()fetch (that's a shared, cached load across the whole app, not owned by any one hook instance). - No built-in retry UI.
naics-search's cache only holds onto a successful load, not a failed one, so remounting the component that callsuseNaicsSearch()(e.g. a Reactkeychange) after an error retries fresh. The hook itself has no manualretry()— it's a one-shot-per-mount wrapper by design.
License
MIT.
