2.0.1 • Published 3 months ago

powerhooks v2.0.1

Weekly downloads
486
License
MIT
Repository
github
Last release
3 months ago

This module is still under development. A real documentation website is coming.

Main hooks

useConstCallback

Believe it or not there is no valid reason to require user to pass in a dependency array to useCallback.

Demo playground to show you why it matters.

useCallbackFactory

To avoid re-rendering every list item component when the parent re-renders.

//Without powerhook:

todos.map(todo => <Todo todo={todo} onClick={(a, b) => onClick(todo, a, b)} />);

//With:

import { useCallbackFactory } from "powerhooks/useCallbackFactory";

//...

const onClickFactory = useCallbackFactory(([todo]: [string], [a, b]: [string, number]) =>
    onClick(todo, a, b)
);

todos.map(todo => <Todo todo={todo} onClick={onClickFactory(todo)} />);

Let's assume <TodoItem /> uses React.memo, in the example without powerhooks, every render of the parent the reference of onComplete changes.
useCallbackFactory on the other hand always returns the same function for a given todo: string.

useGlobalState

Create global state persistent across reloads that is accessible through out the entire app, and this without involving a provider.

NOTE: It makes uses of TypeScript's template literal types to return useIsDarkModeEnabled based on the name parameter ("isDarkModeEnabled").
How cool is that ?!

useIsDarkModeEnabled.ts

import { createUseGlobalState } from "powerhooks/useGlobalState";

export const { useIsDarkModeEnabled, evtIsDarkModeEnabled } = createUseGlobalState({
    name: "isDarkModeEnabled",
    initialState: window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches,
    doPersistAcrossReloads: true
});

MyComponent.tsx

import { useIsDarkModeEnabled } from "./useIsDarkModeEnabled";

export function MyComponent(){

  const { isDarkModeEnabled, setIsDarkModeEnabled }= useIsDarkModeEnabled();

  return (
    <div>
      <p>The dark mode is currently: {isDarkModeEnabled?"enabled":"disabled"}</p>
      <button onClick={()=> setIsDarkModeEnabled(!isDarkModeEnabled)}>
        Click to toggle dark mode
      <button>
    </dvi>
  );

}

Optionally, you can track your state an edit them outside of the react tree React but still trigger refresh when the state is changed.

import { evtIsDarkModeEnabled } from "./useIsDarkModeEnabled";

//After 4 seconds, enable dark mode, it will triggers re-renders of all the components
//that uses the state.
setTimeout(() => {
    evtIsDarkModeEnabled.state = true;
}, 4000);

//Print something in the console anytime the state changes:

evtIsDarkModeEnabled.attach(isDarkModeEnabled => {
    console.log(`idDarkModeEnabled changed, new value: ${isDarkModeEnabled}`);
});

For SSR (Next.js) use powerhook/useSsrGlobalState as showed in src/test/apps/ssr.

useDomRect

Measure rendered components in realtime.

import { useDomRect } from "powerhooks/useDomRect";

function MyComponent() {
    const { ref, domRect } = useDomRect();

    return (
        <>
            <div ref={ref}>
                This div is div size's dimensions <br />
                are determined by it's content
            </div>
            <div
                style={{
                    width: domRect.width,
                    height: domRect.height
                }}
            >
                This div is the same size as the first div
            </div>
        </>
    );
}

WARNING: Not yet compatible with SSR

Used by

Development

Start the test SPA

npx tsc -w
yarn start_spa
2.0.1

3 months ago

2.0.0

3 months ago

1.0.19

7 months ago

1.0.18

7 months ago

1.0.17

7 months ago

1.0.16

7 months ago

1.0.21

4 months ago

1.0.20

5 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.11

1 year ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.10-beta.0

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.3

2 years ago

0.27.2

2 years ago

0.27.1

2 years ago

0.26.9

2 years ago

0.26.15

2 years ago

0.26.16

2 years ago

0.26.11

2 years ago

0.26.12

2 years ago

0.27.0

2 years ago

0.26.13

2 years ago

0.26.14

2 years ago

0.26.10

2 years ago

0.26.8

2 years ago

0.25.0

2 years ago

0.21.0

2 years ago

0.26.3

2 years ago

0.26.2

2 years ago

0.26.1

2 years ago

0.26.0

2 years ago

0.22.1

2 years ago

0.22.0

2 years ago

0.26.7

2 years ago

0.26.6

2 years ago

0.26.4

2 years ago

0.23.0

2 years ago

0.24.0

2 years ago

0.20.31

3 years ago

0.20.32

3 years ago

0.20.30

3 years ago

0.20.28

3 years ago

0.20.29

3 years ago

0.20.26

3 years ago

0.20.27

3 years ago

0.20.24

3 years ago

0.20.25

3 years ago

0.20.22

3 years ago

0.20.23

3 years ago

0.20.21

3 years ago

0.20.20

3 years ago

0.20.19

3 years ago

0.20.17

3 years ago

0.20.18

3 years ago

0.20.15

3 years ago

0.20.16

3 years ago

0.20.13

3 years ago

0.20.14

3 years ago

0.20.1

3 years ago

0.20.0

3 years ago

0.17.2

3 years ago

0.17.0

3 years ago

0.17.1

3 years ago

0.18.0

3 years ago

0.19.0

3 years ago

0.20.11

3 years ago

0.20.12

3 years ago

0.20.10

3 years ago

0.20.9

3 years ago

0.20.8

3 years ago

0.20.7

3 years ago

0.20.6

3 years ago

0.20.5

3 years ago

0.20.4

3 years ago

0.20.3

3 years ago

0.20.2

3 years ago

0.15.0

3 years ago

0.16.3

3 years ago

0.14.0

3 years ago

0.14.1

3 years ago

0.16.0

3 years ago

0.16.1

3 years ago

0.16.2

3 years ago

0.13.0

3 years ago

0.12.0

3 years ago

0.11.2

3 years ago

0.11.1

4 years ago

0.11.0

4 years ago

0.9.7

4 years ago

0.9.4

4 years ago

0.9.3

4 years ago

0.9.6

4 years ago

0.9.5

4 years ago

0.10.0

4 years ago

0.9.2

4 years ago

0.9.0

4 years ago

0.9.1

4 years ago

0.8.0

4 years ago

0.7.2

4 years ago

0.7.3

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.3

4 years ago

0.6.4

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.3.0

4 years ago

0.4.4

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.2.0

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.2

4 years ago

0.1.3

4 years ago

0.0.40

4 years ago

0.0.41

4 years ago

0.0.42

4 years ago

0.0.43

4 years ago

0.0.39

4 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.0.37

4 years ago

0.0.38

4 years ago

0.0.36

4 years ago

0.0.35

4 years ago

0.0.33

4 years ago

0.0.34

4 years ago

0.0.32

4 years ago

0.0.31

4 years ago

0.0.28

4 years ago

0.0.29

4 years ago

0.0.24

4 years ago

0.0.25

4 years ago

0.0.26

4 years ago

0.0.27

4 years ago

0.0.23

4 years ago

0.0.22

4 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.17

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago