Skip to content

Commit

Permalink
better handling of state in search
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwnik committed Jul 17, 2024
1 parent 387886b commit cd9ac77
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions app/components/search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect, useState} from 'react'
import {useEffect, useRef, useState} from 'react'
import {useSearchParams} from '@remix-run/react'
import debounce from 'lodash/debounce'
import {useSearch} from '~/hooks/useSearch'
Expand Down Expand Up @@ -33,15 +33,15 @@ export default function Search({limitFromUrl, className}: Props) {
}
}

// This ugly thing here is so that the useEffect doesn't need `searchFn` as its dependancy
const searchFnRef = useRef(searchFn)
searchFnRef.current = searchFn
useEffect(() => {
const query = searchParams.get('q')
if (loadedQuestions && query) {
searchFn(query)
searchFnRef.current(query)
setShowResults(true)
}
// Properly adding all dependancies would require transforming `searchFn` into a callback
// or something, which in turn would cause this `useEffect` to be called a lot more often.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loadedQuestions, searchParams])

const handleChange = debounce(searchFn, 100)
Expand Down
2 changes: 1 addition & 1 deletion app/routes/questions.toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const loadToC = async (request: any): Promise<LoaderResp> => {
return {
data: data
.filter(canBeShown)
.filter(({tags}) => tags.includes(INTRODUCTORY) || tags.includes(ADVANCED))
.filter(({tags}) => tags?.includes(INTRODUCTORY) || tags?.includes(ADVANCED))
.map(formatQuestion(1))
.sort((a, b) => (a.order || 0) - (b.order || 0)),
timestamp,
Expand Down

0 comments on commit cd9ac77

Please sign in to comment.