4.3.2 • Published 10 days ago

@helux/hooks-impl v4.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 days ago

helux-hooks-impl

useObject

使用普通对象,需注意此接口只接受普通对象,应用里使用 useObject 替代 React.useState 将享受到以下两个好处

  • 方便定义多个状态值时,少写很多 useState
  • 内部做了 unmount 判断,让异步函数也可以安全的调用 setState,避免 react 出现警告 : "Called SetState() on an Unmounted Component" Errors
const [obj, setObj] = useObject({ num: 1, age: 2 });
setObj({ num: 2 }); // 只需要传修改的值即可,内部会自动合并

useMutable

深层次的修改需要使用 useMutable 替代 useObject

const [state, setState] = useMutable({ info: { age: 1 } });
setState((draft) => (draft.info.age = 2));

useEffect

对齐 React.useEffect,但优化了调用逻辑,即 strict 模式与普通模式行为一致,只有一次 mount 与 unmount 产生

import { useEffect } from 'helux';

function Comp() {
  useEffect(() => {
    log('helux.useEffect', 'print one time at strict mode');
    return () => log('helux.useEffect', 'cleanup');
  }, []);
  React.useEffect(() => {
    logRed('React.useEffect', 'print 2 times at strict mode');
    return () => logRed('React.useEffect', 'cleanup');
  }, []);
  return <MarkUpdate>test useEffect</MarkUpdate>;
}

useStable

生成稳定的对象,对象的所有方法将转为稳定引用,且回调里始终可以读到外部的最新值,无闭包陷阱

function Comp(props: any) {
  const [obj, setObj] = useObject({ num: 1 });
  // srv 是稳定的,srv.readState,srv.readProps,srv.changeState 是稳定的
  const srv = useStable({
    readState() {
      console.log(`%c read state num ${obj.num}`, `color:green`);
    },
    readProps() {
      console.log(`%c read props num ${props.num}`, `color:green`);
    },
    changeState() {
      setObj({ num: random() });
    },
  });

  // 传入值,则只是返回最新值
  const numTwo = useStable(2);

  // 如传入单函数,则返回的稳定的函数引用
  const fn = useStable(() => {
    console.log(`%c read state num ${obj.num}`, `color:green`);
  });
}
4.3.2

10 days ago

4.3.1

10 days ago

4.3.0

10 days ago

4.2.7

3 months ago

4.2.6

4 months ago

4.2.5

4 months ago

4.2.4

4 months ago

4.2.3

4 months ago

4.2.2

4 months ago

4.2.1

4 months ago

4.2.0

4 months ago

4.1.6

4 months ago

4.1.5

4 months ago

4.1.4

4 months ago

4.1.3

4 months ago

4.1.2

4 months ago

4.1.1

4 months ago

4.1.0

4 months ago

4.0.3

5 months ago

4.0.2

5 months ago

4.0.1

5 months ago

4.0.0

5 months ago

3.6.14

5 months ago

3.6.13

5 months ago

3.6.12

5 months ago

3.6.11

5 months ago

3.6.10

5 months ago

3.6.6

5 months ago

3.6.5

5 months ago

3.6.9

5 months ago

3.6.8

5 months ago

3.6.7

5 months ago

3.6.2

5 months ago

3.6.1

5 months ago

3.6.4

5 months ago

3.6.3

5 months ago

3.5.23

5 months ago

3.5.22

5 months ago

3.6.0

5 months ago

3.5.21

5 months ago

3.5.19

5 months ago

3.5.18

5 months ago

3.5.17

5 months ago

3.5.16

5 months ago

3.5.20

5 months ago

3.5.15

5 months ago

3.5.14

5 months ago

3.5.13

5 months ago

3.5.12

5 months ago

3.5.11

5 months ago

3.5.10

5 months ago

3.5.7

5 months ago

3.5.6

5 months ago

3.5.9

5 months ago

3.5.8

5 months ago

3.5.5

5 months ago

3.5.4

6 months ago

3.5.3

6 months ago

3.5.2

6 months ago

3.5.1

6 months ago

3.5.0

6 months ago

3.4.25

6 months ago

3.4.26

6 months ago

3.4.24

6 months ago

3.4.20

6 months ago

3.4.21

6 months ago

3.4.22

6 months ago

3.4.23

6 months ago

3.4.16

6 months ago

3.4.17

6 months ago

3.4.18

6 months ago

3.4.19

6 months ago

3.4.9

6 months ago

3.4.14

6 months ago

3.4.15

6 months ago

3.4.10

6 months ago

3.4.11

6 months ago

3.4.12

6 months ago

3.4.13

6 months ago

3.4.7

6 months ago

3.4.6

6 months ago

3.4.5

6 months ago

3.4.4

6 months ago

3.4.3

6 months ago

3.4.2

6 months ago

3.4.1

6 months ago

3.3.8

6 months ago

3.3.7

6 months ago

3.3.6

6 months ago

3.3.5

6 months ago

3.3.4

6 months ago

3.3.3

6 months ago

3.3.2

6 months ago

3.3.1

6 months ago

3.2.7

6 months ago

3.2.6

6 months ago

3.2.5

6 months ago

3.2.2

6 months ago