Lens app source code (#119)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen authored and GitHub committed 2020-03-15 09:52:02 +02:00
1 parent 936cbd53d4
commit 1d0815abd2
797 files changed
+73714 -215

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
// Custom react hooks for common usage
export * from "./useStorage"
export * from "./useOnUnmount"
export * from "./useInterval"
+18
View File
@@ -0,0 +1,18 @@
import { useRef, useEffect } from "react";
export function useInterval(callback: () => void, delay: number) {
const savedCallback = useRef<() => void>();
useEffect(() => {
savedCallback.current = callback;
});
useEffect(() => {
function tick() {
savedCallback.current();
}
const id = setInterval(tick, delay);
return () => clearInterval(id);
}, [delay]);
}
+5
View File
@@ -0,0 +1,5 @@
import { useEffect } from "react";
export function useOnUnmount(callback: () => void) {
useEffect(() => callback, [])
}
+12
View File
@@ -0,0 +1,12 @@
import { useState } from "react";
import { createStorage, IStorageHelperOptions } from "../utils";
export function useStorage<T>(key: string, initialValue?: T, options?: IStorageHelperOptions) {
const storage = createStorage(key, initialValue, options);
const [storageValue, setStorageValue] = useState(storage.get());
const setValue = (value: T) => {
setStorageValue(value);
storage.set(value);
};
return [storageValue, setValue] as [T, (value: T) => void];
}