Some heretical React Hooks.
npm install @hereticaljs/hooks
Yields one value or another.
const x = useConcat(0, 1); // yields 0 and 1Yields every values from a given array.
const xs = useMemo(() => [0, 1, 2], []);
const x = useArray(xs); // yields 0, 1 and 2Treats a generator as a stream and collects values from it.
function* gen() {
yield 0;
return 1;
}
const iter = useMemo(() => gen(), []);
const [value, done] = useGenerator(iter);Filters out undefined values.
function* gen() {
yield 0;
yield undefined;
return 1;
}
const iter = useMemo(() => gen(), []);
const [value, done] = useGenerator(iter);
const x = useDefined(value); // yields 0 and 1Folds over a hook value.
function* gen() {
yield 1;
yield 2;
return 3;
}
const iter = useMemo(() => gen(), []);
const [value, done] = useGenerator(iter);
const x = useFold(value, (a, b) => a + b, 0); // yields 0, 1, 3 and 6Filters out unwanted values.
const mod2 = useCallback(x => x % 2 === 0);
const as = useMemo(() => [0, 1, 2], []);
const x = useArray(as);
const y = useFilter(x, mod2); // yields 0 and 2Resolves a Promise and returns its status.
const p = useMemo(() => api.get('https://example.com'), []);
const [value, error, isPending] = usePromise(p);Chains optional values into another optional value.
const ab = useMaybe(
[a, b],
(a, b) => a * a + b * b,
);Yields values from a given RxJS Observable.
const xs = useMemo(() => of(0, 1, 2), []);
const x = useObservable(xs, 0); // yields 0, 1 and 2Gives a DOMHighResTimeStamp diff from the time it was called first time.
const t = useTime();Gives a number between start and end. It's an application of useTime and it's useful in simple animations.
const theta = useRange(0, 2 * Math.PI);
const x = Math.cos(theta);
const y = Math.sin(theta);
const pt = { x, y };Reads an image from a URL and gives you an ImageData.
const imageData = useImageData('https://example.com/lena.png');Reads a Blob as an ArrayBuffer or a string. Defaults to an ArrayBuffer.
enum ResultType {
ARRAY_BUFFER = 'arraybuffer',
BINARY_STRING = 'binarystring',
DATA_URL = 'dataurl',
TEXT = 'text',
}
const dataurl = useBlob(file, useBlob.ResultType.DATA_URL);Creates an object URL from anything. It's useful with an image blob.
const imageData = useImageData(useObjectURL(file));Gives a ImageData from an image blob. It's an alias of file => useImageData(useObjectURL(file)).
Opens a web socket and streams messages.
const [socket, messages = []] = useWebSocket('wss://echo.websocket.org');
const msgs = messages.filter(x => x).reverse();Binds a state and a state handler to a React element.
const [val, elem] = useProp(element, value, 'value', 'onChange', e => e.target.value);A shortcut to bind a value to an input element.
const [r, rRange] = useInput(
<input type="range" min="0.0" max="1.0" step="0.01" />,
'1.0',
)Stores state histories.
It's named as useSpace instead of useHistory because it flattens a value in time to space(a list).
const [state, setState] = useState(0);
const history = useSpace(state);
useEffect(() => {
setState(s => s + 1);
console.log(history);
}, [history]);- test more hooks
-
useConcat -
useImageData -
useImageFile -
useProp -
useInput -
useObjectURL -
useTime -
useRange
-
- rewrite everything in TypeScript