我正在使用Algolia的即时搜索,我想知道当我点击hits小部件上的“点击”时,我可以使用什么代码将我发送到特定的页面。我在用Next.js。
代码:
import React from 'react';
import { useRef, useState, useEffect } from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch } from 'react-instantsearch-dom';
import { Index } from 'react-instantsearch-dom';
import { Configure } from 'react-instantsearch-dom';
import { Pagination } from 'react-instantsearch-dom';
const searchClient = algoliasearch(
'XXXXXXXXXX',
'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
);
const Hit = ({ hit }) => <p>{hit.title}</p>;
import { connectSearchBox } from 'react-instantsearch-dom';
const SearchBox = ({ currentRefinement, isSearchStalled, refine }) => (
<form noValidate action="" role="search">
<div className="container flex justify-center items-center px-4 sm:px-6 lg:px-8 relative">
<input
type="search"
placeholder='Search Documentation'
value={currentRefinement}
onChange={event => refine(event.currentTarget.value)}
className="h-7 w-96 pr-8 pl-5 rounded z-0 hover:text-gray-500 outline-none border-b-2"
/>
<i className="fa fa-search text-gray-400 z-20 hover:text-gray-500"></i>
</div>
<button onClick={() => refine('')}>Reset query</button>
{isSearchStalled ? 'My search is stalled' : ''}
</form>
);
const CustomSearchBox = connectSearchBox(SearchBox);
import { connectHits } from 'react-instantsearch-dom';
const Hits = ({ hits }) => (
<table className="table-auto">
{hits.map(hit => (
<tbody>
<tr>
<td className="text-black font-bold" key={hit.objectID}>{hit.title}</td>
</tr>
</tbody>
))}
</table>
);
const CustomHits = connectHits(Hits);
import { QueryRuleCustomData } from 'react-instantsearch-dom';
function SearchApp({location, history}) {
const [showHits, setShowHits] = useState(false);
return (
<div>
<>
<InstantSearch
indexName="prod_Directory"
searchClient={searchClient}
>
<Index indexName="prod_Directory">
{/* Widgets */}
<div>
<CustomSearchBox onFocus={()=>setShowHits(true)} onBlur={()=>setShowHits(false)}/>
<CustomHits className="table-auto"/>
{/*
{showHits ? <CustomHits className="table-auto"/> : null}
*/}
</div>
</Index>
<Configure hitsPerPage={2} />
<QueryRuleCustomData
transformItems={items => {
const match = items.find(data => Boolean(data.redirect));
if (match && match.redirect) {
window.location.href = match.redirect;
}
return [];
}}
>
{() => null}
</QueryRuleCustomData>
</InstantSearch>
</>
</div>
)
}
export default SearchApp
我在阿尔戈里亚的医生里什么都找不到。再次,我希望能够点击我的一个点击,并让它重定向或路由我到一个特定的页面。
发布于 2021-12-17 07:53:23
我找到了答案:
import router, {useRouter} from "next/router";
import { connectHits } from 'react-instantsearch-dom';
const Hits = ({ hits }) => (
<table className="table-auto">
{hits.map(hit => (
<tbody>
<tr>
<td className="text-black font-bold" key={hit.objectID} onClick={() => router.push(hit.url)}>{hit.title}</td>
</tr>
</tbody>
))}
</table>
);
const CustomHits = connectHits(Hits);
在您的搜索记录中(我使用了Algolia索引来创建我的搜索记录),您只需要在JSON记录中的url中编写代码,然后在hit.url属性上使用react路由器!
发布于 2021-12-16 08:04:17
看起来这里使用的是自定义点击小部件,而不是开箱即用的instantsearch.js小部件(这很好)。
您需要在点击模板中构建链接:
const Hits = ({
hits
}) => ( <
table className = "table-auto" > {
hits.map(hit => ( <
tbody >
<
tr >
<
td className = "text-black font-bold"
key = {
hit.objectID
} > {
hit.title
} < /td> <
/tr>
<
/tbody>
))
} <
/table>
);
例如,如果将URL存储在对象记录中,则可以执行以下操作:
{
<a href="hit.url">hit.title</a>
}
更有可能的是,您希望使用Link构建onClick事件。类似于:
<Link
onClick={() => {
setIsOpen(false);
}}
to={`/product/${hit.objectID}`}
>
hit.title
</Link>
在这两种情况下,只需确保构建链接所需的一切(URL、路由In等)。嵌入在Algolia记录中,然后按照应用程序的一般情况,在命中模板中构建链接。
https://stackoverflow.com/questions/70375927
复制相似问题