From f2745d713c8b1035ecf987a10cb8bc753d6c9fbf Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sat, 26 Mar 2022 14:21:36 +0300 Subject: [PATCH] feat: useNoInitialEffect in CommonDisplay (fix #14) --- src/web/search/CommonDisplay.tsx | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/web/search/CommonDisplay.tsx b/src/web/search/CommonDisplay.tsx index a4e5666..a7c7f4e 100644 --- a/src/web/search/CommonDisplay.tsx +++ b/src/web/search/CommonDisplay.tsx @@ -1,5 +1,5 @@ import { useRouter } from 'next/router'; -import React, { Fragment, useEffect, useState } from 'react'; +import React, { DependencyList, EffectCallback, Fragment, useEffect, useState } from 'react'; import ButtonsForStars from '../display/buttonsForStars'; import Form from '../display/form'; @@ -44,6 +44,17 @@ export const defaultQueryParameters: QueryParametersWithoutNum = { }; export const defaultNumDisplay = 21; +const useNoInitialEffect = (effect: EffectCallback, deps: DependencyList) => { + const initial = React.useRef(true); + useEffect(() => { + if (initial.current) { + initial.current = false; + return; + } + return effect(); + }, deps); +}; + /* Body */ const CommonDisplay: React.FC = ({ defaultResults, @@ -159,9 +170,9 @@ const CommonDisplay: React.FC = ({ }); }; - useEffect(updateRoute, [numDisplay]); + useNoInitialEffect(updateRoute, [numDisplay]); - useEffect(() => { + useNoInitialEffect(() => { setResults([]); let newTimeoutId = setTimeout(() => { updateRoute();