ts
    箭头函数写泛型
    const useFilters = (
    一定要将T exteds 一下才可以,因为不这样做,无法与 jsx 识别区分开来,有点无奈。
    https://github.com/Microsoft/TypeScript/issues/4922
    image.png

    ts
    https://zhuanlan.zhihu.com/p/214280582
    灵活获取组件入口类型
    常适用于库的作者未能描述正确入口的类型的情况。
    不用导出,获取一个组件的类型:

    1. import * as React from 'react';
    2. import { Button } from 'antd';
    3. type ButtonProps = React.ComponentProps<typeof Button>;

    如果某个组件用泛型描述,通过添加一个泛型工具函数:

    1. export interface Type<T> extends Function {
    2. new (...args: any[]): T;
    3. }
    4. type NumberSelectProps = React.ComponentProps<Type<Select<number>>>;

    forceUpdate
    在hook中,useState的dispatch可以触发rerender
    所以可以利用其特性,配合 ref 进行自定义的数据更改后render:

    1. /**
    2. * Execute code before next frame but async
    3. */
    4. export function useLayoutState<State>(
    5. defaultState: State,
    6. ): [State, (updater: Updater<State>) => void] {
    7. const stateRef = useRef(defaultState);
    8. const [, forceUpdate] = useState({});
    9. const lastPromiseRef = useRef<Promise<void>>(null);
    10. const updateBatchRef = useRef<Updater<State>[]>([]);
    11. function setFrameState(updater: Updater<State>) {
    12. updateBatchRef.current.push(updater);
    13. const promise = Promise.resolve();
    14. lastPromiseRef.current = promise;
    15. promise.then(() => {
    16. if (lastPromiseRef.current === promise) {
    17. const prevBatch = updateBatchRef.current;
    18. const prevState = stateRef.current;
    19. updateBatchRef.current = [];
    20. prevBatch.forEach(batchUpdater => {
    21. stateRef.current = batchUpdater(stateRef.current);
    22. });
    23. lastPromiseRef.current = null;
    24. if (prevState !== stateRef.current) {
    25. forceUpdate({});
    26. }
    27. }
    28. });
    29. }
    30. useEffect(
    31. () => () => {
    32. lastPromiseRef.current = null;
    33. },
    34. [],
    35. );
    36. return [stateRef.current, setFrameState];
    37. }