Skip to main content

Hooks

React hooks used to access the state and actions of the API.

useFastState()

The useFastState hook exposes the inner state of the API.

interface FastStateProps {
isReady: boolean
isLoading: IsLoading,
results?: FullTextResults | smartCollectionsResults
query: string
page: number,
narrow?: Narrow[]
type: AppType
collectionID?: string
sortBy: SortBy
recommendations?: {
[id: string]: Widget
},
singleProducts?: {
[id: string]: Product
}
autocomplete?: AutocompleteResults
searchWithinSearch?: string
}

const {query, page, ...state} = useFastState();

useFastStateFunctions()

The useFastStateFunctions hook exposes actions to directly manipulate the state of the API. Upon setting the inner state, a new search request will be sent.

interface FastStateFunctionsProps {
setQuery: (query: string) => void,
setSort: (sortBy: SortBy) => void,
setNarrow: (narrow?: Narrow[]) => void,
setPage: (page: number) => void,
getAllCategories: () => Promise<FastCategory[]>
setCollection: (id: string) => void
setState: (newState: SetStateProps) => void
fetchRecommnedetions: ({productID, specs}: { productID: string, specs: Specs[] }) => void
fetchSingleProducts: (ids: string[]) => void
fetchAutocomplete: (query: string) => void
setSearchWithinSearch: (query: string) => void
}

const {setQuery, setSort, setNarrow, setPage} = useFastStateFunctions();

useFastCategories()

The useFastCategories hook brings a dictenory structure of:

  • url_path to FastCategory
  • category_id to FastCategory
interface SiteCategories {
[urlOrID: string]: FastCategory,
}
const categories:SiteCategories = useFastCategories();

useRouting()

The useRouting hook saves the state in the URL, that way after a refresh / clicking back the setup is saved

interface useRoutingProps{
queryParamName?: string // custom ?q= param (default="q")
pageParamName?: string // custom ?page= param (default="p")
narrowParamName?: string // custom ?narrow= param (default="n")
sortParamName?: string // custom ?sort= param (default="s")
searchPath?: string, // custom search path (default="")
keepParams?: string[] // choose params you want to keep in the url forever (default=[])
}
...
export default function App(){
useRouting();
...
}
...