Skip to content

Commit

Permalink
caching the built value of options by useMemo
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Kyrylenko committed Dec 14, 2020
1 parent dfe9af1 commit 119d81f
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions components/ProductSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
//@ts-ignore
import Select from 'react-select';
import { useRouter } from 'next/router';
Expand All @@ -10,7 +10,8 @@ const ProductSelect = ({ selectedProduct }: { selectedProduct?: string }) => {
const router = useRouter();
const { locale } = router;
const { t } = useTranslation();
const options = Object.keys(products).map(p => ({ value: p, label: t(p) }));
//caching the built value of options by useMemo https://app.pluralsight.com/course-player?clipId=bcc762de-e49c-4ddd-9658-d29f4f8ebe29
const options = useMemo(() => Object.keys(products).map(p => ({ value: p, label: t(p) })), [locale]);

const handleChange = (option: any) => router.push(option.value, option.value, { locale: locale });

Expand Down

1 comment on commit 119d81f

@vercel
Copy link

@vercel vercel bot commented on 119d81f Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.