Skip to content

Commit

Permalink
Fix Featured Product Search not working for large stores (woocommerce…
Browse files Browse the repository at this point in the history
…#5156)

* Remove the dependency from the getProducts useEffect

* using ref instead of empty dependency array for useEffect
  • Loading branch information
alexflorisca authored and jonny-bull committed Dec 14, 2021
1 parent 67ad929 commit e75e532
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions assets/js/hocs/with-searched-products.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 } )
Expand Down

0 comments on commit e75e532

Please sign in to comment.