-
I've just added a full text search feature using React instantsearch dom. The current code is this↓. import React, { useState, useEffect } from "react"
import { Link } from 'react-router-dom';
import { db } from "../../../firebaseConfig"
import {
collection,
orderBy,
query,
getDocs,
limit
} from 'firebase/firestore';
import 'firebase/compat/auth'
import MainTopImg from '../../../images/imecon-personal-color.jpeg'
import Header from "../../block/Header"
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, SearchBox, Hits } from 'react-instantsearch-dom';
const MobileContents = () => {
const [list, setList] = useState([]);
const client = algoliasearch(
process.env.REACT_APP_ALGOLIA_ID,
process.env.REACT_APP_ADMIN_API_KEY
);
useEffect(() => {
(async () => {
const salons = query(collection(db, "salon"), orderBy("createAt", "desc"), limit(5));
const querySnapshot = await getDocs(salons);
let salonList = [];
await querySnapshot.forEach((doc) => {
console.log(doc._document.data.value.mapValue.fields)
salonList = [...salonList, {
salonId: doc._document.data.value.mapValue.fields.salonId.stringValue,
salonName: doc._document.data.value.mapValue.fields.salonName.stringValue,
}]
});
setList(salonList)
})();
}, []);
function HitComponent(hit) {
console.log(hit.hit)
return (
<div>
<Link to={'salon/' + hit.hit.hit.salonId}>
<p className="hover:text-[#06bbbc]">{hit.hit.hit.salonName}</p>
</Link>
</div>
);
}
const hitComponent = (hit) => (
<HitComponent hit={hit} onClick={() => null} />
);
return (
<div>
<Header />
<div>
<img src={MainTopImg} alt="mainTopImg" style={{maxWidth: 400 + 'px'}} />
<Link to="/mypage">MyPage</Link>
<div>
<h3>Serach</h3>
<div>
<InstantSearch indexName={"image-consult-app"} searchClient={client}>
<SearchBox />
<Hits hitComponent={hitComponent}/>
</InstantSearch>
</div>
</div>
<div>
<h3>New Salon List</h3>
{list.map((data) => {
return (
<li key={data.salonId}>
<Link to={'/salon/' + data.salonId}>
<p>Salon Name:{data.salonName}</p>
</Link>
</li>
)
})}
</div>
</div>
</div>
);
}
export default MobileContents; |
Beta Was this translation helpful? Give feedback.
Answered by
Haroenv
Jan 28, 2022
Replies: 0 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ohanamaru
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<SearchBox searchAsYouType={false} />
https://www.algolia.com/doc/api-reference/widgets/search-box/react/#widget-param-searchasyoutype