mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
refactor: 💡 use import hooks from React proper
This commit is contained in:
parent
5d7434867c
commit
cdc5ec596d
@ -1,7 +1,7 @@
|
||||
import {storiesOf} from '@storybook/react';
|
||||
import * as React from 'react';
|
||||
import {useAdopt} from '..';
|
||||
import {useState, useCallback} from '../react';
|
||||
import {useState} from 'react';
|
||||
import ShowDocs from '../util/ShowDocs';
|
||||
import {Spring} from 'react-spring';
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import * as React from 'react';
|
||||
import {storiesOf} from '@storybook/react';
|
||||
import {useGetSet} from '..';
|
||||
import {useState} from '../react';
|
||||
import {useState} from 'react';
|
||||
import ShowDocs from '../util/ShowDocs';
|
||||
|
||||
const Demo = () => {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import {storiesOf} from '@storybook/react';
|
||||
import * as React from 'react';
|
||||
import {useRef} from '../react';
|
||||
import {useRef} from 'react';
|
||||
import {useHoverDirty} from '..';
|
||||
import ShowDocs from '../util/ShowDocs';
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import * as React from 'react';
|
||||
import {storiesOf} from '@storybook/react';
|
||||
import {useOutsideClick} from '..';
|
||||
import {useRef} from '../react';
|
||||
import {useRef} from 'react';
|
||||
import ShowDocs from '../util/ShowDocs';
|
||||
|
||||
const Demo = () => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useMemo} from './react';
|
||||
import {useMemo} from 'react';
|
||||
|
||||
const createMemo = fn => (...args) => useMemo(() => fn(...args), args);
|
||||
|
||||
|
||||
17
src/react.ts
17
src/react.ts
@ -1,17 +0,0 @@
|
||||
import * as React from 'react';
|
||||
|
||||
export type UseState = <T>(initialState: T | (() => T)) => [T, (newState: T | ((newState) => T)) => void];
|
||||
export const useState: UseState = (React as any).useState;
|
||||
|
||||
export type UseEffect = (didUpdate: () => ((() => void) | void), params?: any[]) => void;
|
||||
export const useEffect: UseEffect = (React as any).useEffect;
|
||||
|
||||
export interface ReactRef<T> {current: T};
|
||||
export type UseRef = <T>(initialValue: T) => ReactRef<T>;
|
||||
export const useRef: UseRef = (React as any).useRef;
|
||||
|
||||
export type UseCallback = <T extends ((...args: any[]) => any)>(callback: T, args: any[]) => T;
|
||||
export const useCallback: UseCallback = (React as any).useCallback;
|
||||
|
||||
export type UseMemo = <T>(fn: Function, args: any[]) => T;
|
||||
export const useMemo: UseMemo = (React as any).useMemo;
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect, useCallback} from './react';
|
||||
import {useState, useEffect, useCallback} from 'react';
|
||||
|
||||
export interface AsyncState<T> {
|
||||
loading: boolean;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
import {on, off} from './util';
|
||||
|
||||
export interface BatterySensorState {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import useGetSet from './useGetSet';
|
||||
import {useCallback} from './react';
|
||||
import {useCallback} from 'react';
|
||||
|
||||
export interface CounterActions {
|
||||
inc: (delta?: number) => void;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
const {create} = require('nano-css');
|
||||
const {addon: addonCssom} = require('nano-css/addon/cssom');
|
||||
const {addon: addonPipe} = require('nano-css/addon/pipe');
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useEffect} from './react';
|
||||
import {useEffect} from 'react';
|
||||
|
||||
const useFavicon = (href: string) => {
|
||||
useEffect(() => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
|
||||
export interface GeoLocationSensorState {
|
||||
accuracy: number,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useRef, useCallback} from './react';
|
||||
import {useRef, useCallback} from 'react';
|
||||
import useUpdate from './useUpdate';
|
||||
|
||||
const useGetSet = <T>(initialValue: T): [() => T, (value: T) => void] => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useRef, useCallback} from './react';
|
||||
import {useRef, useCallback} from 'react';
|
||||
import useUpdate from './useUpdate';
|
||||
|
||||
const useGetSetState = <T extends object>(initialState: T = {} as T): [() => T, (patch: Partial<T>) => void]=> {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import * as React from 'react';
|
||||
import {useState} from './react';
|
||||
|
||||
const {useState} = React;
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useEffect, useState} from './react';
|
||||
import {useEffect, useState} from 'react';
|
||||
|
||||
// kudos: https://usehooks.com/
|
||||
const useHoverDirty = (ref) => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
import {on, off} from './util';
|
||||
import {throttle} from 'throttle-debounce';
|
||||
|
||||
@ -23,7 +23,7 @@ const useIdle = (ms: number = oneMinute, initialState: boolean = false, events:
|
||||
if (localState) {
|
||||
set(false);
|
||||
}
|
||||
|
||||
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(() => set(true), ms);
|
||||
});
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useEffect} from './react';
|
||||
import {useEffect} from 'react';
|
||||
|
||||
const useLifecycles = (mount, unmount?) => {
|
||||
useEffect(() => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState} from './react';
|
||||
import {useState} from 'react';
|
||||
|
||||
export interface Actions<T> {
|
||||
set: (list: T[]) => void;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
|
||||
const isClient = typeof window === 'object';
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
import {isClient, on, off} from './util';
|
||||
|
||||
const patchHistoryMethod = (method) => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useEffect} from './react';
|
||||
import {useEffect} from 'react';
|
||||
import useLifecycles from './useLifecycles';
|
||||
|
||||
const useLogger = (name, props) => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState} from './react';
|
||||
import {useState} from 'react';
|
||||
|
||||
export interface Actions<K, V> {
|
||||
get: (key: K) => any;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
|
||||
const useMedia = (query: string, defaultState: boolean = false) => {
|
||||
const [state, setState] = useState(defaultState);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
import {on, off} from './util';
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
import {on, off} from './util';
|
||||
|
||||
export interface MotionSensorState {
|
||||
@ -50,7 +50,7 @@ const useMotion = (initialState: MotionSensorState = defaultState) => {
|
||||
rotationRate,
|
||||
interval
|
||||
} = event;
|
||||
|
||||
|
||||
setState({
|
||||
acceleration: {
|
||||
x: acceleration.x,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useEffect} from './react';
|
||||
import {useEffect} from 'react';
|
||||
|
||||
const useMount = (mount) => useEffect(mount, []);
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
import {on, off} from './util';
|
||||
|
||||
export interface NetworkState {
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
|
||||
const useObservable = <T>(observable$, initialValue?: T): T | undefined => {
|
||||
const [value, update] = useState<T | undefined>(initialValue);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
const s = observable$.subscribe(update)
|
||||
return () => s.unsubscribe();
|
||||
}, [observable$]);
|
||||
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
import {on, off} from './util';
|
||||
|
||||
export interface OrientationState {
|
||||
@ -20,11 +20,11 @@ const useOrientation = (initialState: OrientationState = defaultState) => {
|
||||
const onChange = () => {
|
||||
if (mounted) {
|
||||
const {orientation} = screen as any;
|
||||
|
||||
|
||||
if (!orientation) {
|
||||
setState(initialState);
|
||||
}
|
||||
|
||||
|
||||
const {angle, type} = orientation;
|
||||
setState({angle, type});
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useLayoutEffect} from './react';
|
||||
import {useState, useLayoutEffect} from 'react';
|
||||
|
||||
const useRaf = (ms: number = 1e12, delay: number = 0): number => {
|
||||
const [elapsed, set] = useState<number>(0);
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import * as React from 'react';
|
||||
import {useState, useCallback} from './react';
|
||||
import createMemo from './createMemo';
|
||||
|
||||
const {useState, useCallback} = React;
|
||||
|
||||
const useRenderProp = (element: React.ReactElement<any>): [React.ReactElement<any>, any[]] => {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (!React.isValidElement(element)) {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
|
||||
const isClient = typeof window === 'object';
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState} from './react';
|
||||
import {useState} from 'react';
|
||||
|
||||
const useSetState = <T extends object>(initialState: T = {} as T): [T, (patch: Partial<T> | Function) => void]=> {
|
||||
const [state, set] = useState<T>(initialState);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import * as React from 'react';
|
||||
import {useState, useEffect, useRef} from './react';
|
||||
|
||||
const {useState, useEffect, useRef} = React;
|
||||
|
||||
const isClient = typeof window === 'object';
|
||||
const DRAF = (callback: () => void) => setTimeout(callback, 35);
|
||||
@ -25,7 +26,7 @@ const useSize = (element: Element, {width = Infinity, height = Infinity}: Partia
|
||||
if (typeof element === 'function') {
|
||||
element = element(state);
|
||||
}
|
||||
|
||||
|
||||
const style = element.props.style || {};
|
||||
const ref = useRef<HTMLIFrameElement | null>(null);
|
||||
let window: Window | null = null;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useRef} from './react';
|
||||
import {useRef} from 'react';
|
||||
import useSetState from './useSetState';
|
||||
import useMount from './useMount';
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import {SpringSystem, Spring} from 'rebound';
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
|
||||
const useSpring = (targetValue: number = 0, tension: number = 50, friction: number = 3) => {
|
||||
const [spring, setSpring] = useState<Spring | null>(null);
|
||||
@ -32,7 +32,7 @@ const useSpring = (targetValue: number = 0, tension: number = 50, friction: numb
|
||||
spring.setEndValue(targetValue);
|
||||
}
|
||||
}, [targetValue]);
|
||||
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
|
||||
const useTimeout = (ms: number = 0) => {
|
||||
const [ready, setReady] = useState(false);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useEffect} from './react';
|
||||
import {useEffect} from 'react';
|
||||
|
||||
const useTitle = (title: string) => {
|
||||
useEffect(() => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState} from './react';
|
||||
import {useState} from 'react';
|
||||
|
||||
export type UseToggle = (state: boolean) => [
|
||||
boolean, // state
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useEffect} from './react';
|
||||
import {useEffect} from 'react';
|
||||
|
||||
const useUnmount = (unmount) => {
|
||||
useEffect(() => () => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState} from './react';
|
||||
import {useState} from 'react';
|
||||
|
||||
const useUpdate = () => useState(0)[1] as (() => void);
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { Waiter, useWait } from 'react-wait'
|
||||
import {Waiter, useWait} from 'react-wait';
|
||||
|
||||
useWait.Waiter = Waiter;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {useState, useEffect} from './react';
|
||||
import {useState, useEffect} from 'react';
|
||||
|
||||
const isClient = typeof window === 'object';
|
||||
|
||||
@ -7,7 +7,7 @@ const useWindowSize = (initialWidth = Infinity, initialHeight = Infinity) => {
|
||||
width: isClient ? window.innerWidth : initialWidth,
|
||||
height: isClient ? window.innerHeight : initialHeight,
|
||||
});
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
const handler = () => {
|
||||
setState({
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import * as React from 'react';
|
||||
import {useEffect, useRef, ReactRef} from '../react';
|
||||
import {useEffect, useRef} from 'react';
|
||||
import useSetState from '../useSetState';
|
||||
import parseTimeRanges from './parseTimeRanges';
|
||||
|
||||
@ -26,7 +26,7 @@ export interface HTMLMediaControls {
|
||||
}
|
||||
|
||||
const createHTMLMediaHook = (tag: 'audio' | 'video') => {
|
||||
const hook = (elOrProps: HTMLMediaProps | React.ReactElement<HTMLMediaProps>): [React.ReactElement<HTMLMediaProps>, HTMLMediaState, HTMLMediaControls, ReactRef<HTMLAudioElement | null>] => {
|
||||
const hook = (elOrProps: HTMLMediaProps | React.ReactElement<HTMLMediaProps>): [React.ReactElement<HTMLMediaProps>, HTMLMediaState, HTMLMediaControls, {current: HTMLAudioElement | null}] => {
|
||||
let element: React.ReactElement<any> | undefined;
|
||||
let props: HTMLMediaProps;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user