Use useMemo in a proper way in ClientContext

This commit is contained in:
Daniel Abramov 2023-07-03 18:45:42 +01:00
parent 6998555ad9
commit 647599a39e

View File

@ -22,6 +22,7 @@ import {
createContext, createContext,
useContext, useContext,
useRef, useRef,
useMemo,
} from "react"; } from "react";
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import { MatrixClient } from "matrix-js-sdk/src/client"; import { MatrixClient } from "matrix-js-sdk/src/client";
@ -263,11 +264,9 @@ export const ClientProvider: FC<Props> = ({ children }) => {
}, [initClientState?.client, setAlreadyOpenedErr, t]) }, [initClientState?.client, setAlreadyOpenedErr, t])
); );
const [state, setState] = useState<ClientState | undefined>(undefined); const state: ClientState = useMemo(() => {
useEffect(() => {
if (alreadyOpenedErr) { if (alreadyOpenedErr) {
setState({ state: "error", error: alreadyOpenedErr }); return { state: "error", error: alreadyOpenedErr };
return;
} }
let authenticated = undefined; let authenticated = undefined;
@ -280,7 +279,7 @@ export const ClientProvider: FC<Props> = ({ children }) => {
}; };
} }
setState({ state: "valid", authenticated, setClient }); return { state: "valid", authenticated, setClient };
}, [alreadyOpenedErr, changePassword, initClientState, logout, setClient]); }, [alreadyOpenedErr, changePassword, initClientState, logout, setClient]);
useEffect(() => { useEffect(() => {