feat: useNoInitialEffect in CommonDisplay (fix #14)

This commit is contained in:
Vyacheslav Matyukhin 2022-03-26 14:21:36 +03:00
parent 1b1c0f4228
commit f2745d713c
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C

View File

@ -1,5 +1,5 @@
import { useRouter } from 'next/router'; 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 ButtonsForStars from '../display/buttonsForStars';
import Form from '../display/form'; import Form from '../display/form';
@ -44,6 +44,17 @@ export const defaultQueryParameters: QueryParametersWithoutNum = {
}; };
export const defaultNumDisplay = 21; 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 */ /* Body */
const CommonDisplay: React.FC<Props> = ({ const CommonDisplay: React.FC<Props> = ({
defaultResults, defaultResults,
@ -159,9 +170,9 @@ const CommonDisplay: React.FC<Props> = ({
}); });
}; };
useEffect(updateRoute, [numDisplay]); useNoInitialEffect(updateRoute, [numDisplay]);
useEffect(() => { useNoInitialEffect(() => {
setResults([]); setResults([]);
let newTimeoutId = setTimeout(() => { let newTimeoutId = setTimeout(() => {
updateRoute(); updateRoute();