0.0.14 • Published 2 years ago

@jrop/hooks v0.0.14

Weekly downloads
1
License
ISC
Repository
-
Last release
2 years 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

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.9

2 years ago

0.0.8

3 years ago

0.0.5

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

4 years ago

0.0.2

5 years ago

0.0.1

5 years ago