Skip to content

Commit 9916c3a

Browse files
committed
feat: improve perfs with use-context-selector
1 parent 5c909f6 commit 9916c3a

File tree

7 files changed

+46
-17
lines changed

7 files changed

+46
-17
lines changed

frontend/components/game/article.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
parse,
66
} from "node-html-parser";
77
import React, { Key, ReactNode, useContext, useEffect, useMemo } from "react";
8+
import { useContextSelector } from "use-context-selector";
89

910
import CustomGameBanner from "@caviardeul/components/game/customGameBanner";
1011
import { GameContext } from "@caviardeul/components/game/gameManager";
@@ -18,7 +19,10 @@ import {
1819
} from "@caviardeul/utils/caviarding";
1920

2021
const WordContainer_: React.FC<{ word: string }> = ({ word }) => {
21-
const { revealedWords, isOver, selection } = useContext(GameContext);
22+
const [isOver, revealedWords, selection] = useContextSelector(
23+
GameContext,
24+
(context) => [context.isOver, context.revealedWords, context.selection],
25+
);
2226
if (word === undefined) {
2327
return null;
2428
}
@@ -118,7 +122,10 @@ const parseText = (text: string): ReactNode => {
118122
};
119123

120124
const ArticleContainer = () => {
121-
const { article, selection } = useContext(GameContext);
125+
const [article, selection] = useContextSelector(GameContext, (context) => [
126+
context.article,
127+
context.selection,
128+
]);
122129
const { settings } = useContext(SettingsContext);
123130
const { autoScroll } = settings;
124131

frontend/components/game/attemptHistory.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import React, { useContext, useEffect, useRef } from "react";
1+
import React, { useEffect, useRef } from "react";
2+
import { useContextSelector } from "use-context-selector";
23

34
import { GameContext } from "@caviardeul/components/game/gameManager";
45

56
const AttemptHistory = () => {
6-
const { isOver, history, selection, updateSelection } =
7-
useContext(GameContext);
7+
const [isOver, history, selection, updateSelection] = useContextSelector(
8+
GameContext,
9+
(context) => [
10+
context.isOver,
11+
context.history,
12+
context.selection,
13+
context.updateSelection,
14+
],
15+
);
816
const selectedRow = useRef<HTMLTableRowElement>(null);
917
const selectedWord = selection ? selection[0] : null;
1018

frontend/components/game/gameInformation.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import Link from "next/link";
2-
import React, { useContext } from "react";
2+
import React from "react";
3+
import { useContextSelector } from "use-context-selector";
34

45
import { GameContext } from "@caviardeul/components/game/gameManager";
56
import ExternalLink from "@caviardeul/components/utils/externalLink";
67
import ShareAction from "@caviardeul/components/utils/shareAction";
78

89
const GameInformation = () => {
9-
const { article, userScore, isOver, history } = useContext(GameContext);
10+
const [article, userScore, isOver, history] = useContextSelector(
11+
GameContext,
12+
(context) => [
13+
context.article,
14+
context.userScore,
15+
context.isOver,
16+
context.history,
17+
],
18+
);
1019
if (!isOver || !article) {
1120
return null;
1221
}

frontend/components/game/gameManager.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import React, {
2-
createContext,
3-
useCallback,
4-
useEffect,
5-
useMemo,
6-
useState,
7-
} from "react";
1+
import React, { useCallback, useEffect, useMemo, useState } from "react";
2+
import { createContext } from "use-context-selector";
83

94
import { saveGameScore } from "@caviardeul/lib/queries";
105
import { Article, GameHistory } from "@caviardeul/types";

frontend/components/game/input.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import React, { ChangeEvent, useCallback, useContext, useState } from "react";
1+
import React, { ChangeEvent, useCallback, useState } from "react";
22
import { FaArrowUp } from "react-icons/fa";
3+
import { useContextSelector } from "use-context-selector";
34

45
import { GameContext } from "@caviardeul/components/game/gameManager";
56
import { isWord, splitWords } from "@caviardeul/utils/caviarding";
67

78
const Input = () => {
8-
const { canPlay, makeAttempt } = useContext(GameContext);
9+
const [canPlay, makeAttempt] = useContextSelector(GameContext, (context) => [
10+
context.canPlay,
11+
context.makeAttempt,
12+
]);
913
const [value, setValue] = useState<string>("");
1014
const [lastValue, setLastValue] = useState<string>("");
1115

frontend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"react": "19.0.0-rc.1",
2121
"react-dom": "19.0.0-rc.1",
2222
"react-icons": "^5.0.1",
23-
"swr": "^2.2.4"
23+
"swr": "^2.2.4",
24+
"use-context-selector": "^2.0.0"
2425
},
2526
"devDependencies": {
2627
"@eslint/compat": "^1.2.3",

frontend/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,11 @@ [email protected]:
28042804
resolved "https://registry.yarnpkg.com/urlsafe-base64/-/urlsafe-base64-1.0.0.tgz#23f89069a6c62f46cf3a1d3b00169cefb90be0c6"
28052805
integrity sha512-RtuPeMy7c1UrHwproMZN9gN6kiZ0SvJwRaEzwZY0j9MypEkFqyBaKv176jvlPtg58Zh36bOkS0NFABXMHvvGCA==
28062806

2807+
use-context-selector@^2.0.0:
2808+
version "2.0.0"
2809+
resolved "https://registry.yarnpkg.com/use-context-selector/-/use-context-selector-2.0.0.tgz#3b5dafec7aa947c152d4f0aa7f250e99a205df3d"
2810+
integrity sha512-owfuSmUNd3eNp3J9CdDl0kMgfidV+MkDvHPpvthN5ThqM+ibMccNE0k+Iq7TWC6JPFvGZqanqiGCuQx6DyV24g==
2811+
28072812
use-sync-external-store@^1.2.0:
28082813
version "1.2.2"
28092814
resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.2.tgz#c3b6390f3a30eba13200d2302dcdf1e7b57b2ef9"

0 commit comments

Comments
 (0)