Fix Featured Product Search not working for large stores (https://github.com/woocommerce/woocommerce-blocks/pull/5156)

* Remove the dependency from the getProducts useEffect

* using ref instead of empty dependency array for useEffect
This commit is contained in:
Alex Florisca 2021-12-02 18:51:46 +00:00 committed by GitHub
parent 12bb68d970
commit e8b4b3467c
1 changed files with 5 additions and 3 deletions

View File

@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { useEffect, useState, useCallback } from '@wordpress/element';
import { useEffect, useState, useCallback, useRef } from '@wordpress/element';
import { blocksConfig } from '@woocommerce/block-settings';
import { getProducts } from '@woocommerce/editor-components/utils';
import { useDebouncedCallback } from 'use-debounce';
@ -41,14 +41,16 @@ const withSearchedProducts = (
setIsLoading( false );
};
const selectedRef = useRef( selected );
useEffect( () => {
getProducts( { selected } )
getProducts( { selected: selectedRef.current } )
.then( ( results ) => {
setProductsList( results as ProductResponseItem[] );
setIsLoading( false );
} )
.catch( setErrorState );
}, [ selected ] );
}, [ selectedRef ] );
const debouncedSearch = useDebouncedCallback( ( search: string ) => {
getProducts( { selected, search } )