Skip to content

Commit 32fe9d0

Browse files
committed
refactor(programs): implement stateless architecture for dropdown using useMemo
Signed-off-by: Rishi Raj <rishiraj438gt@gmail.com>
1 parent a228a3a commit 32fe9d0

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/templates/program-multiple.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useMemo } from "react";
22
import { graphql } from "gatsby";
33

44
import SEO from "../components/seo";
@@ -27,17 +27,17 @@ const ProgramsPage = ({ data, children, pageContext }) => {
2727
const programs = data.allMdx.nodes;
2828
const { navigate } = require("gatsby");
2929

30-
const [activeOption] = useState(() => {
30+
const activeOption = useMemo(() => {
3131
const initialIndex = programs.findIndex((program) => program.fields.slug === pageContext.slug);
3232
return initialIndex !== -1 ? initialIndex : 0;
33-
});
33+
}, [programs, pageContext.slug]);
3434

35-
const options = programs.map((program) => {
35+
const options = useMemo(() => programs.map((program) => {
3636
let optionItem = new Object();
3737
optionItem.label = program.frontmatter.title;
3838
optionItem.value = program.fields.slug;
3939
return optionItem;
40-
});
40+
}), [programs]);
4141
return (
4242

4343
<>

0 commit comments

Comments
 (0)