Replace useContext with use

The docs recommend the use hook because it is simpler and allows itself to be called conditionally.
This commit is contained in:
Robin
2025-06-23 22:57:39 -04:00
parent f86c9fe0a0
commit d4b38563e7
5 changed files with 10 additions and 11 deletions

View File

@@ -11,7 +11,7 @@ import {
useEffect, useEffect,
useState, useState,
createContext, createContext,
useContext, use,
useRef, useRef,
useMemo, useMemo,
type JSX, type JSX,
@@ -69,8 +69,7 @@ const ClientContext = createContext<ClientState | undefined>(undefined);
export const ClientContextProvider = ClientContext.Provider; export const ClientContextProvider = ClientContext.Provider;
export const useClientState = (): ClientState | undefined => export const useClientState = (): ClientState | undefined => use(ClientContext);
useContext(ClientContext);
export function useClient(): { export function useClient(): {
client?: MatrixClient; client?: MatrixClient;

View File

@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details. Please see LICENSE in the repository root for full details.
*/ */
import { createContext, useContext, useMemo } from "react"; import { createContext, use, useMemo } from "react";
import { useObservableEagerState } from "observable-hooks"; import { useObservableEagerState } from "observable-hooks";
import { type MediaDevices } from "./state/MediaDevices"; import { type MediaDevices } from "./state/MediaDevices";
@@ -15,7 +15,7 @@ export const MediaDevicesContext = createContext<MediaDevices | undefined>(
); );
export function useMediaDevices(): MediaDevices { export function useMediaDevices(): MediaDevices {
const mediaDevices = useContext(MediaDevicesContext); const mediaDevices = use(MediaDevicesContext);
if (mediaDevices === undefined) if (mediaDevices === undefined)
throw new Error( throw new Error(
"useMediaDevices must be used within a MediaDevices context provider", "useMediaDevices must be used within a MediaDevices context provider",

View File

@@ -24,7 +24,7 @@ import {
createContext, createContext,
forwardRef, forwardRef,
memo, memo,
useContext, use,
useEffect, useEffect,
useMemo, useMemo,
useRef, useRef,
@@ -124,7 +124,7 @@ interface LayoutContext {
const LayoutContext = createContext<LayoutContext | null>(null); const LayoutContext = createContext<LayoutContext | null>(null);
function useLayoutContext(): LayoutContext { function useLayoutContext(): LayoutContext {
const context = useContext(LayoutContext); const context = use(LayoutContext);
if (context === null) if (context === null)
throw new Error("useUpdateLayout called outside a Grid layout context"); throw new Error("useUpdateLayout called outside a Grid layout context");
return context; return context;

View File

@@ -14,7 +14,7 @@ import {
createContext, createContext,
type FC, type FC,
type JSX, type JSX,
useContext, use,
useEffect, useEffect,
useMemo, useMemo,
} from "react"; } from "react";
@@ -34,7 +34,7 @@ type ProcessorState = {
const ProcessorContext = createContext<ProcessorState | undefined>(undefined); const ProcessorContext = createContext<ProcessorState | undefined>(undefined);
export function useTrackProcessor(): ProcessorState { export function useTrackProcessor(): ProcessorState {
const state = useContext(ProcessorContext); const state = use(ProcessorContext);
if (state === undefined) if (state === undefined)
throw new Error( throw new Error(
"useTrackProcessor must be used within a ProcessorProvider", "useTrackProcessor must be used within a ProcessorProvider",

View File

@@ -8,7 +8,7 @@ Please see LICENSE in the repository root for full details.
import { EventType, RelationType } from "matrix-js-sdk"; import { EventType, RelationType } from "matrix-js-sdk";
import { import {
createContext, createContext,
useContext, use,
type ReactNode, type ReactNode,
useCallback, useCallback,
useMemo, useMemo,
@@ -34,7 +34,7 @@ const ReactionsSenderContext = createContext<
>(undefined); >(undefined);
export const useReactionsSender = (): ReactionsSenderContextType => { export const useReactionsSender = (): ReactionsSenderContextType => {
const context = useContext(ReactionsSenderContext); const context = use(ReactionsSenderContext);
if (!context) { if (!context) {
throw new Error("useReactions must be used within a ReactionsProvider"); throw new Error("useReactions must be used within a ReactionsProvider");
} }