mirror of https://github.com/vector-im/riot-web
improve defaults for useRoomState and useStateToggle hooks
parent
8d4ac90265
commit
13a2f779b9
|
@ -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(() => {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue