0.0.14 • Published 6 months ago

@jrop/hooks v0.0.14

Weekly downloads
1
License
ISC
Repository
-
Last release
6 months ago

@jrop's React Hooks

pipeline status

useAsync

useControlled

function useControlled<T>(prop: T|undefined, initialValue: T|(() => T), onChange?: (x: T) => any)

A handy utility for creating optional prop-controlled components. Uses React.useState, internally.

For example:

const MyTextInput = (props: {value?: string, onValueChanged?: (newValue: string) => any}) => {
  const [value, setValue] = useControlled(props.value, "", props.onValueChanged);
  // any calls to setValue will automatically call onValueChanged
  // will work for controlled/uncontrolled mode (i.e., whether or not props.value is provided)
  // ...
}

useControlledReducer

function useControlledReducer<A, T>(prop: T|undefined, initialValue: T|(() => T), reducer: (prevState: T, action: A) => T, onChange?: (x: T) => any)

A handy utility for creating optional prop-controlled components. Uses React.useReducer, internally.

For example:

const MyTextInput = (props: {value?: string, onValueChanged?: (newValue: string) => any}) => {
  const [value, dispatch] = useControlledReducer(props.value, "", (_state, action: string) => action, props.onValueChanged);
  // any calls to dispatch will automatically call onValueChanged
  // will work for controlled/uncontrolled mode (i.e., whether or not props.value is provided)
  // ...
}

useMounted

function useMounted(): boolean

Returns whether the component is mounted.

usePopup

useWrappedCallback

function useWrappedCallback<F extends Function>(f: F): F

Wraps a function whose identity does not change across re-renders. That is, for:

const fn = useWrappedCallback(() => {});

...fn will always be the same reference.

0.0.14-next.0

6 months ago

0.0.10

7 months ago

0.0.11

7 months ago

0.0.12

7 months ago

0.0.13

7 months ago

0.0.14

7 months ago

0.0.9

7 months ago

0.0.8

1 year ago

0.0.5

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.4

2 years ago

0.0.3

3 years ago

0.0.2

4 years ago

0.0.1

4 years ago