/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/dataviews/src/hooks/use-elements.ts
59 строк
1 KB
Marco Ciampini
ESLint: Replace strict config with bulk suppressions (#81248)
07 авг 2026, 14:34
Не верифицирован
07 авг 2026, 14:34
7f43eaf
Код
Авторство
О чём код?
import { useEffect, useState } from '@wordpress/element'; import type { Option } from '../types'; const EMPTY_ARRAY: Option[] = []; export default function useElements( { elements, getElements, }: { elements?: Option[]; getElements?: () => Promise< Option[] >; } ) { const staticElements = Array.isArray( elements ) && elements.length > 0 ? elements : EMPTY_ARRAY; const [ records, setRecords ] = useState< Option[] >( staticElements ); const [ isLoading, setIsLoading ] = useState( false ); useEffect( () => { if ( ! getElements ) { setRecords( staticElements ); return; } let cancelled = false; setIsLoading( true ); getElements() .then( ( fetchedElements ) => { if ( ! cancelled ) { const dynamicElements = Array.isArray( fetchedElements ) && fetchedElements.length > 0 ? fetchedElements : staticElements; setRecords( dynamicElements ); } } ) .catch( () => { if ( ! cancelled ) { setRecords( staticElements ); } } ) .finally( () => { if ( ! cancelled ) { setIsLoading( false ); } } ); return () => { cancelled = true; }; }, [ getElements, staticElements ] ); return { elements: records, isLoading, }; }