How to use the react-hook-form.useFormContext function in react-hook-form

To help you get started, we’ve selected a few react-hook-form examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github react-hook-form / react-hook-form-input / src / index.tsx View on Github external
onChangeEvent,
  onBlurName,
  onBlurEvent,
  ...rest
}: Props) => {
  const isCheckbox = type === 'checkbox';
  const isOnChange = mode === 'onChange';
  const isOnBlur = mode === 'onBlur';
  const defaultData = isCheckbox
    ? isUndefined(defaultChecked)
      ? false
      : defaultChecked
    : defaultValue;
  const [inputValue, setInputValue] = React.useState(defaultData);
  const valueRef = React.useRef(defaultData);
  const methods = useFormContext() || {};
  const setValue = setValueFromProp || methods.setValue;
  const register = registerFromProp || methods.register;
  const unregister = unregisterFromProp || methods.unregister;

  const commonTask = (target: any) => {
    const data = getValue(target, { isCheckbox });
    setInputValue(data);
    valueRef.current = data;
    return data;
  };

  const eventWrapper = (event: EventFunction, eventName: string) => {
    return async (...arg: any) => {
      const data = commonTask(await event(arg));
      setValue(
        name,
github react-hook-form / react-hook-form / examples / formContext.tsx View on Github external
function Test() {
  const data = useFormContext();
  return <input name="bill">;
}

react-hook-form

Performant, flexible and extensible forms library for React Hooks

MIT
Latest version published 11 days ago

Package Health Score

95 / 100
Full package analysis