Skip to content

Instantly share code, notes, and snippets.

@bramses
Last active March 22, 2022 06:45
Show Gist options
  • Select an option

  • Save bramses/0e8f528aa7fada51f1fcbddca0cbe3bd to your computer and use it in GitHub Desktop.

Select an option

Save bramses/0e8f528aa7fada51f1fcbddca0cbe3bd to your computer and use it in GitHub Desktop.
how im using algolia in nextjs with styled components
import { connectStateResults } from "react-instantsearch-dom";
import Link from "next/link";
import { StyledTitleLink } from "../components/StyledTitleLink";
import CustomHighlight from "./Highlight";
function Hits({ searchState, searchResults }) {
const validQuery = searchState.query?.length >= 1; // 1 is the minimum query length
return (
<>
{searchResults?.hits.length === 0 && validQuery && ( // no results found in a valid query
<p>No results found!</p>
)}
{searchResults?.hits.length > 0 && ( // show hits
<>
{searchResults.hits.map((hit, index) => (
<div tabIndex={index} key={hit.objectID}>
<>
<Link href={`/projects/${hit.slug}`} passHref>
<StyledTitleLink>
<CustomHighlight attribute="title" hit={hit} />
</StyledTitleLink>
</Link>
<CustomHighlight attribute="excerpt" hit={hit} />
</>
</div>
))}
</>
)}
</>
);
}
export default connectStateResults(Hits);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment