Skip to content

Commit

Permalink
use memo istead of state and useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
madhusudhand committed Dec 6, 2024
1 parent 6279914 commit d1ab480
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions packages/design-picker/src/components/unified-design-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { recordTracksEvent } from '@automattic/calypso-analytics';
import { Button } from '@automattic/components';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useCallback, useMemo, useRef, useState } from 'react';
import { useInView } from 'react-intersection-observer';
import { SHOW_ALL_SLUG } from '../constants';
import { useFilteredDesigns } from '../hooks/use-filtered-designs';
Expand All @@ -15,7 +15,7 @@ import NoResults from './no-results';
import PatternAssemblerCta, { usePatternAssemblerCtaData } from './pattern-assembler-cta';
import ThemeCard from './theme-card';
import type { Categorization } from '../hooks/use-categorization';
import type { Category, Design, StyleVariation } from '../types';
import type { Design, StyleVariation } from '../types';
import type { RefCallback } from 'react';
import './style.scss';

Expand Down Expand Up @@ -227,26 +227,21 @@ const DesignPicker: React.FC< DesignPickerProps > = ( {
} ) => {
const hasCategories = !! Object.keys( categorization?.categories || {} ).length;
const filteredDesigns = useFilteredDesigns( designs, categorization );
const features = [ 'blog', 'magazine', 'portfolio', 'podcast', 'store' ];
const [ featureCategories, setFeatureCategories ] = useState< Category[] >( [] );
const [ subjectCategories, setSubjectCategories ] = useState< Category[] >( [] );
const features = [ 'blog', 'portfolio', 'podcast', 'store' ];
const featureCategories = useMemo(
() => ( categorization?.categories || [] ).filter( ( { slug } ) => features.includes( slug ) ),
[ categorization?.categories ]
);
const subjectCategories = useMemo(
() =>
( categorization?.categories || [] ).filter( ( { slug } ) => ! features.includes( slug ) ),
[ categorization?.categories ]
);

const assemblerCtaData = usePatternAssemblerCtaData();

const translate = useTranslate();

useEffect( () => {
if ( ! categorization?.categories?.length ) {
return;
}
setFeatureCategories(
categorization.categories.filter( ( { slug } ) => features.includes( slug ) )
);
setSubjectCategories(
categorization.categories.filter( ( { slug } ) => ! features.includes( slug ) )
);
}, [ categorization?.categories ] );

return (
<div>
<div className="design-picker__filters">
Expand Down

0 comments on commit d1ab480

Please sign in to comment.