fix(sessions): stabilize setAllSessions to prevent polling loop
Wrap setAllSessions in useCallback so it has a stable reference. This breaks the infinite re-render loop that was causing 4-10 requests per second to /users/me/sessions.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { createContext, useContext, useState, type ReactNode } from "react";
|
import { createContext, useCallback, useContext, useState, type ReactNode } from "react";
|
||||||
|
|
||||||
export interface Session {
|
export interface Session {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -24,9 +24,9 @@ const SessionsContext = createContext<SessionsContextType | undefined>(undefined
|
|||||||
export const SessionsProvider = ({ children }: { children: ReactNode }) => {
|
export const SessionsProvider = ({ children }: { children: ReactNode }) => {
|
||||||
const [sessions, setSessions] = useState<Session[]>([]);
|
const [sessions, setSessions] = useState<Session[]>([]);
|
||||||
|
|
||||||
const setAllSessions = (newSessions: Session[]) => {
|
const setAllSessions = useCallback((newSessions: Session[]) => {
|
||||||
setSessions(newSessions);
|
setSessions(newSessions);
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SessionsContext.Provider value={{ sessions, setAllSessions }}>
|
<SessionsContext.Provider value={{ sessions, setAllSessions }}>
|
||||||
|
|||||||
Reference in New Issue
Block a user