mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Lens app source code (#119)
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
936cbd53d4
commit
1d0815abd2
797 files changed
+73714
-215
No files matched your search
@@ -0,0 +1,5 @@
|
||||
// Custom react hooks for common usage
|
||||
|
||||
export * from "./useStorage"
|
||||
export * from "./useOnUnmount"
|
||||
export * from "./useInterval"
|
||||
@@ -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]);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { useEffect } from "react";
|
||||
|
||||
export function useOnUnmount(callback: () => void) {
|
||||
useEffect(() => callback, [])
|
||||
}
|
||||
@@ -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];
|
||||
}
|
||||
Reference in new issue
Block a user