1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-11-07 10:46:24 +03:00

improve defaults for useRoomState and useStateToggle hooks

This commit is contained in:
Michael Telatynski
2021-06-08 16:29:06 +01:00
parent 8d4ac90265
commit 13a2f779b9
2 changed files with 5 additions and 2 deletions

View File

@@ -20,8 +20,11 @@ import { RoomState } from "matrix-js-sdk/src/models/room-state";
import { useEventEmitter } from "./useEventEmitter";
type Mapper<T> = (roomState: RoomState) => T;
const defaultMapper: Mapper<RoomState> = (roomState: RoomState) => roomState;
// Hook to simplify watching Matrix Room state
export const useRoomState = <T extends any>(room: Room, mapper: (state: RoomState) => T): T => {
export const useRoomState = <T extends any = RoomState>(room: Room, mapper: Mapper<T> = defaultMapper): T => {
const [value, setValue] = useState<T>(room ? mapper(room.currentState) : undefined);
const update = useCallback(() => {