Last active
March 22, 2022 06:45
-
-
Save bramses/0e8f528aa7fada51f1fcbddca0cbe3bd to your computer and use it in GitHub Desktop.
how im using algolia in nextjs with styled components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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