Skip to content

Commit d35b0e0

Browse files
authored
Merge pull request #529 from krishitag/fix/issue-471-usememo-usecallback
Add useMemo/useCallback to context providers, fix broken react depend…
2 parents 248d939 + 5c07e22 commit d35b0e0

6 files changed

Lines changed: 182 additions & 94 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"prettier": "^3.5.3",
6565
"prettier-plugin-tailwindcss": "^0.6.11",
6666
"raw-loader": "^4.0.2",
67-
"react": "link:@embedpdf/plugin-viewport/react",
67+
"react": "^18.3.1",
6868
"react-beautiful-dnd": "^13.1.1",
6969
"react-dom": "^18.3.1",
7070
"react-dropzone": "^14.3.8",

‎pnpm-lock.yaml‎

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/components/Card.tsx‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ interface CardProps {
2828
const Card = ({ paper, onSelect, isSelected, isShow=true }: CardProps) => {
2929
const [previewOpen, setPreviewOpen] = React.useState(false);
3030

31+
const paperContextValue = React.useMemo(
32+
() => ({
33+
paperId: paper._id,
34+
subject: paper.subject,
35+
exam: paper.exam,
36+
slot: paper.slot,
37+
year: paper.year,
38+
}),
39+
[paper._id, paper.subject, paper.exam, paper.slot, paper.year],
40+
);
41+
3142
React.useEffect(() => {
3243
if (!previewOpen) return;
3344

@@ -147,15 +158,7 @@ const Card = ({ paper, onSelect, isSelected, isShow=true }: CardProps) => {
147158
<div
148159
className="relative mx-auto h-full max-w-[760px]"
149160
>
150-
<PaperProvider
151-
value={{
152-
paperId: paper._id,
153-
subject: paper.subject,
154-
exam: paper.exam,
155-
slot: paper.slot,
156-
year: paper.year,
157-
}}
158-
>
161+
<PaperProvider value={paperContextValue}>
159162
<button
160163
className="fixed right-4 top-4 z-[60] rounded-full border border-black/10 bg-white/95 p-2 text-black shadow-md transition hover:bg-white"
161164
onClick={() => setPreviewOpen(false)}

‎src/components/ui/carousel.tsx‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,32 @@ const Carousel = React.forwardRef<
119119
};
120120
}, [api, onSelect]);
121121

122+
const contextValue = React.useMemo(
123+
() => ({
124+
carouselRef,
125+
api,
126+
opts,
127+
orientation:
128+
orientation || (opts?.axis === "y" ? "vertical" : "horizontal"),
129+
scrollPrev,
130+
scrollNext,
131+
canScrollPrev,
132+
canScrollNext,
133+
}),
134+
[
135+
carouselRef,
136+
api,
137+
opts,
138+
orientation,
139+
scrollPrev,
140+
scrollNext,
141+
canScrollPrev,
142+
canScrollNext,
143+
],
144+
);
145+
122146
return (
123-
<CarouselContext.Provider
124-
value={{
125-
carouselRef,
126-
api: api,
127-
opts,
128-
orientation:
129-
orientation || (opts?.axis === "y" ? "vertical" : "horizontal"),
130-
scrollPrev,
131-
scrollNext,
132-
canScrollPrev,
133-
canScrollNext,
134-
}}
135-
>
147+
<CarouselContext.Provider value={contextValue}>
136148
<div
137149
ref={ref}
138150
onKeyDownCapture={handleKeyDown}

‎src/context/courseContext.tsx‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
"use client";
22

3-
import React, { createContext, useContext, useEffect, useState } from "react";
3+
import React, {
4+
createContext,
5+
useCallback,
6+
useContext,
7+
useEffect,
8+
useMemo,
9+
useState,
10+
} from "react";
411
import axios from "axios";
512
import {
613
type ICourse,
@@ -24,7 +31,7 @@ export function CoursesProvider({ children }: { children: React.ReactNode }) {
2431
const [loading, setLoading] = useState(true);
2532
const [error, setError] = useState<string | null>(null);
2633

27-
const fetchCourses = async () => {
34+
const fetchCourses = useCallback(async () => {
2835
setLoading(true);
2936
setError(null);
3037
try {
@@ -56,16 +63,19 @@ export function CoursesProvider({ children }: { children: React.ReactNode }) {
5663
} finally {
5764
setLoading(false);
5865
}
59-
};
66+
}, []);
6067

6168
useEffect(() => {
6269
void fetchCourses();
63-
}, []);
70+
}, [fetchCourses]);
71+
72+
const value = useMemo(
73+
() => ({ courses, loading, error, refetch: fetchCourses }),
74+
[courses, loading, error, fetchCourses],
75+
);
6476

6577
return (
66-
<CoursesContext.Provider
67-
value={{ courses, loading, error, refetch: fetchCourses }}
68-
>
78+
<CoursesContext.Provider value={value}>
6979
{children}
7080
</CoursesContext.Provider>
7181
);

0 commit comments

Comments
 (0)