Download wp-calypso/client/sites/hooks/use-initialize-dataviews-page.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/sites/hooks/use-initialize-dataviews-page.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/sites/hooks/use-initialize-dataviews-page.ts
-
curl -L -o use-initialize-dataviews-page.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/sites/hooks/use-initialize-dataviews-page.ts
1.15 kB
| import { usePrevious } from '@wordpress/compose'; | |
| import { useEffect, useRef } from 'react'; | |
| import type { View } from '@wordpress/dataviews'; | |
| // DataViews' pagination always resets when the search component is mounted, even though the search term has not changed. | |
| // This is a bug which has a fix in https://github.com/WordPress/gutenberg/pull/61307. | |
| // This is a workaround until the above fix is released. | |
| // Here, we restore the page to the previous page if it is unintentionally changed by the above bug. | |
| export function useInitializeDataViewsPage< V extends View >( | |
| dataViewsState: V, | |
| setDataViewsState: ( state: V ) => void | |
| ) { | |
| const prevPage = usePrevious( dataViewsState.page ); | |
| const prevSearch = usePrevious( dataViewsState.search ); | |
| const done = useRef( false ); | |
| useEffect( () => { | |
| if ( prevPage === 1 ) { | |
| done.current = true; | |
| } | |
| if ( done.current ) { | |
| return; | |
| } | |
| if ( dataViewsState.search === prevSearch && dataViewsState.page !== prevPage ) { | |
| setDataViewsState( { | |
| ...dataViewsState, | |
| page: prevPage, | |
| } ); | |
| done.current = true; | |
| } | |
| }, [ dataViewsState, prevPage, prevSearch, setDataViewsState ] ); | |
| } | |