用于在 React 开发者工具中显示自定义 hook 的标签

    1. import React, { useState, useDebugValue } from 'react';
    2. import ReactDOM from 'react-dom';
    3. // 自定义 Hook
    4. function useMyCount(num) {
    5. const [ count, setCount ] = useState(0);
    6. // 延迟格式化
    7. useDebugValue(count > num ? '溢出' : '不足', status => {
    8. return status === '溢出' ? 1 : 0;
    9. });
    10. const myCount = () => {
    11. setCount(count + 2);
    12. }
    13. return [ count, myCount ];
    14. }
    15. function App() {
    16. const [ count, seCount ] = useMyCount(10);
    17. return (
    18. <div>
    19. {count}
    20. <button onClick={() => seCount()}>setCount</button>
    21. </div>
    22. )
    23. }
    24. ReactDOM.render(<App />, root);

    :::info 不推荐你向每个自定义 Hook 使用 useDebugValue,只有自定义 Hook 被复用时才最有意义。 :::