improve defaults for useRoomState and useStateToggle hooks

pull/21833/head
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(() => {

View File

@ -18,7 +18,7 @@ import {Dispatch, SetStateAction, useState} from "react";
// Hook to simplify toggling of a boolean state value
// Returns value, method to toggle boolean value and method to set the boolean value
export const useStateToggle = (initialValue: boolean): [boolean, () => void, Dispatch<SetStateAction<boolean>>] => {
export const useStateToggle = (initialValue = false): [boolean, () => void, Dispatch<SetStateAction<boolean>>] => {
const [value, setValue] = useState(initialValue);
const toggleValue = () => {
setValue(!value);