From 378803a83d6b866256e3facd9c3e88fe13dd8064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20G=C3=B3mez?= Date: Tue, 10 Nov 2020 16:44:47 +0100 Subject: [PATCH] Wrap pagination handler with a `useCallback` --- .../public/components/log_stream/index.tsx | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/x-pack/plugins/infra/public/components/log_stream/index.tsx b/x-pack/plugins/infra/public/components/log_stream/index.tsx index 45ecf9d5b536cf..20a9fb84b14f0b 100644 --- a/x-pack/plugins/infra/public/components/log_stream/index.tsx +++ b/x-pack/plugins/infra/public/components/log_stream/index.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { useMemo } from 'react'; +import React, { useMemo, useCallback } from 'react'; import { noop } from 'lodash'; import { useMount } from 'react-use'; import { euiStyled } from '../../../../observability/public'; @@ -97,13 +97,33 @@ Read more at https://github.com/elastic/kibana/blob/master/src/plugins/kibana_re [entries] ); + const parsedHeight = typeof height === 'number' ? `${height}px` : height; + // Component lifetime useMount(() => { loadSourceConfiguration(); fetchEntries(); }); - const parsedHeight = typeof height === 'number' ? `${height}px` : height; + // Pagination handler + const handlePagination = useCallback( + ({ fromScroll, pagesBeforeStart, pagesAfterEnd }) => { + if (!fromScroll) { + return; + } + + if (isLoadingMore) { + return; + } + + if (pagesBeforeStart < PAGE_THRESHOLD) { + fetchPreviousEntries(); + } else if (pagesAfterEnd < PAGE_THRESHOLD) { + fetchNextEntries(); + } + }, + [isLoadingMore, fetchPreviousEntries, fetchNextEntries] + ); return ( @@ -120,21 +140,7 @@ Read more at https://github.com/elastic/kibana/blob/master/src/plugins/kibana_re isStreaming={false} lastLoadedTime={null} jumpToTarget={noop} - reportVisibleInterval={({ fromScroll, pagesBeforeStart, pagesAfterEnd }) => { - if (!fromScroll) { - return; - } - - if (isLoadingMore) { - return; - } - - if (pagesBeforeStart < PAGE_THRESHOLD) { - fetchPreviousEntries(); - } else if (pagesAfterEnd < PAGE_THRESHOLD) { - fetchNextEntries(); - } - }} + reportVisibleInterval={handlePagination} loadNewerItems={noop} reloadItems={fetchEntries} highlightedItem={highlight ?? null}