Convert hooks to Typescript

pull/21833/head
Michael Telatynski 2020-08-04 22:00:05 +01:00
parent 93b4ea5be4
commit ed4a427287
4 changed files with 14 additions and 9 deletions

View File

@ -14,9 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { useState, useEffect } from 'react';
import {useState, useEffect, DependencyList} from 'react';
export const useAsyncMemo = (fn, deps, initialValue) => {
type Fn<T> = () => Promise<T>;
export const useAsyncMemo = <T>(fn: Fn<T>, deps: DependencyList, initialValue?: T) => {
const [value, setValue] = useState(initialValue);
useEffect(() => {
fn().then(setValue);

View File

@ -15,11 +15,14 @@ limitations under the License.
*/
import {useRef, useEffect} from "react";
import type {EventEmitter} from "events";
type Handler = (...args: any[]) => void;
// Hook to wrap event emitter on and removeListener in hook lifecycle
export const useEventEmitter = (emitter, eventName, handler) => {
export const useEventEmitter = (emitter: EventEmitter, eventName: string | symbol, handler: Handler) => {
// Create a ref that stores handler
const savedHandler = useRef();
const savedHandler = useRef(handler);
// Update ref.current value if handler changes.
useEffect(() => {

View File

@ -18,7 +18,7 @@ import {useEffect, useState} from "react";
import SettingsStore from '../settings/SettingsStore';
// Hook to fetch the value of a setting and dynamically update when it changes
export const useSettingValue = (settingName, roomId = null, excludeDefault = false) => {
export const useSettingValue = (settingName: string, roomId: string = null, excludeDefault = false) => {
const [value, setValue] = useState(SettingsStore.getValue(settingName, roomId, excludeDefault));
useEffect(() => {
@ -35,7 +35,7 @@ export const useSettingValue = (settingName, roomId = null, excludeDefault = fal
};
// Hook to fetch whether a feature is enabled and dynamically update when that changes
export const useFeatureEnabled = (featureName, roomId = null) => {
export const useFeatureEnabled = (featureName: string, roomId: string = null) => {
const [enabled, setEnabled] = useState(SettingsStore.isFeatureEnabled(featureName, roomId));
useEffect(() => {

View File

@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import {useState} from 'react';
import {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) => {
const [value, setValue] = useState(Boolean(initialValue));
export const useStateToggle = (initialValue: boolean) => {
const [value, setValue] = useState(initialValue);
const toggleValue = () => {
setValue(!value);
};