Skip to content

Commit

Permalink
memoize context value to avoid it changing each render
Browse files Browse the repository at this point in the history
  • Loading branch information
vuolen committed Aug 22, 2024
1 parent 68e7f9f commit c5fd23f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/LanguageContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { createContext, useContext, useState, ReactNode } from "react";
import React, {
createContext,
useContext,
useState,
ReactNode,
useMemo,
} from "react";

export type Language = "fi" | "sv" | "en";

Expand All @@ -16,9 +22,13 @@ export const LanguageProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const [language, setLanguage] = useState<Language>("en");
const value = useMemo(
() => ({ language, setLanguage }),
[language, setLanguage],
);

return (
<LanguageContext.Provider value={{ language, setLanguage }}>
<LanguageContext.Provider value={value}>
{children}
</LanguageContext.Provider>
);
Expand Down

0 comments on commit c5fd23f

Please sign in to comment.