mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
commit
49ea508405
@ -1,3 +1,4 @@
|
||||
module.exports = {
|
||||
presets: ['@babel/preset-env', '@babel/preset-react', "@babel/preset-typescript"],
|
||||
plugins: ['@babel/plugin-syntax-dynamic-import']
|
||||
};
|
||||
@ -44,6 +44,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/streamich/react-use#readme",
|
||||
"dependencies": {
|
||||
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
|
||||
"copy-to-clipboard": "^3.1.0",
|
||||
"nano-css": "^5.1.0",
|
||||
"react-fast-compare": "^2.0.4",
|
||||
|
||||
35
src/__tests__/useObservable.test.tsx
Normal file
35
src/__tests__/useObservable.test.tsx
Normal file
@ -0,0 +1,35 @@
|
||||
import { act, renderHook } from 'react-hooks-testing-library';
|
||||
import { Subject } from 'rxjs';
|
||||
import { useObservable } from '..';
|
||||
|
||||
test('default initial value is undefined', () => {
|
||||
const subject$ = new Subject();
|
||||
const { result } = renderHook(() => useObservable(subject$));
|
||||
|
||||
expect(result.current).toBe(undefined);
|
||||
});
|
||||
|
||||
test('can specify initial value', () => {
|
||||
const subject$ = new Subject();
|
||||
const { result } = renderHook(() => useObservable(subject$, 123));
|
||||
|
||||
expect(result.current).toBe(123);
|
||||
});
|
||||
|
||||
test('returns the latest value of observables', () => {
|
||||
const subject$ = new Subject();
|
||||
const { result } = renderHook(() => useObservable(subject$, 123));
|
||||
|
||||
act(() => {
|
||||
subject$.next(125);
|
||||
});
|
||||
expect(result.current).toBe(125);
|
||||
|
||||
act(() => {
|
||||
subject$.next(300);
|
||||
subject$.next(400);
|
||||
});
|
||||
expect(result.current).toBe(400);
|
||||
});
|
||||
|
||||
xtest('subscribes to observable only once', () => {});
|
||||
@ -1,6 +1,16 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
const useObservable = <T>(observable$, initialValue?: T): T | undefined => {
|
||||
export interface Observable<T> {
|
||||
subscribe: (
|
||||
listener: (value: T) => void
|
||||
) => {
|
||||
unsubscribe: () => void;
|
||||
};
|
||||
}
|
||||
|
||||
function useObservable<T>(observable$: Observable<T>): T | undefined;
|
||||
function useObservable<T>(observable$: Observable<T>, initialValue: T): T;
|
||||
function useObservable<T>(observable$: Observable<T>, initialValue?: T): T | undefined {
|
||||
const [value, update] = useState<T | undefined>(initialValue);
|
||||
|
||||
useEffect(() => {
|
||||
@ -9,6 +19,6 @@ const useObservable = <T>(observable$, initialValue?: T): T | undefined => {
|
||||
}, [observable$]);
|
||||
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
export default useObservable;
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
const useSetState = <T extends object>(initialState: T = {} as T): [T, (patch: Partial<T> | ((prevState: T) => Partial<T>)) => void] => {
|
||||
const useSetState = <T extends object>(
|
||||
initialState: T = {} as T
|
||||
): [T, (patch: Partial<T> | ((prevState: T) => Partial<T>)) => void] => {
|
||||
const [state, set] = useState<T>(initialState);
|
||||
const setState = patch => {
|
||||
set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user