Skip to content

Releases: react-hook-form/lenses

v0.7.1

13 Jun 07:10
ba280c2
Compare
Choose a tag to compare

0.7.1 (2025-06-13)

Bug Fixes

  • links in package json and readme (#22) (ba280c2)

v0.7.0

10 Jun 08:07
3c00e80
Compare
Choose a tag to compare

0.7.0 (2025-06-10)

Features

v0.6.0

22 Apr 19:41
2a58f72
Compare
Choose a tag to compare

0.6.0 (2025-04-22)

Features

v0.5.0

06 Apr 13:43
4203ead
Compare
Choose a tag to compare

0.5.0 (2025-04-06)

Features

  • Remove lens from cache on control unregister (#11) (4203ead)

v0.4.0

30 Mar 13:25
836fa44
Compare
Choose a tag to compare

0.4.0 (2025-03-30)

Features

  • Allow lens fields spreading in reflect callback (#12) (836fa44)

v0.3.0

19 Mar 09:39
84e5a47
Compare
Choose a tag to compare

0.3.0 (2025-03-19)

Features

  • Allow array reflection (#7)
  • Update lenses storage (#8)
  • Remove join method (#9)
  • Pass array item to lens map callback (#10) (84e5a47)

v0.2.0

05 Mar 06:56
7716194
Compare
Choose a tag to compare

0.2.0 (2025-03-05)

Features

  • Fix behaviour of union type. (#3) (d5a0290)

Thanks @horita-yuya

Version 0.1.1

26 Feb 10:32
Compare
Choose a tag to compare

🎊 Features

  • Type-Safe Form State: Focus on specific parts of your form state with full TypeScript support and precise type inference
  • Functional Lenses: Build complex form state transformations through composable lens operations
  • Deep Structure Support: Handle deeply nested structures and arrays elegantly with specialized array operations
  • Seamless Integration: Work smoothly with React Hook Form's Control API and existing functionality
  • Optimized Performance: Each lens is cached and reused for optimal performance
  • Array Handling: Specialized support for array fields with type-safe mapping
  • Composable API: Build complex form state transformations through lens composition

⏰ Quickstart

import { useFieldArray, useForm } from 'react-hook-form';
import { Lens, useLens } from '@hookform/lenses';

function FormComponent() {
  const { handleSubmit, control } = useForm<{
    firstName: string;
    lastName: string;
    children: {
      name: string;
      surname: string;
    }[];
  }>({});

  const lens = useLens({ control });
  const children = lens.focus('children');
  const { fields: childrenFields } = useFieldArray(children.interop());

  return (
    <form onSubmit={handleSubmit(console.log)}>
      <PersonForm
        lens={lens.reflect((l) => ({
          name: l.focus('firstName'),
          surname: l.focus('lastName'),
        }))}
      />

      {children.map(childrenFields, (l, key) => (
        <PersonForm key={key} lens={l} />
      ))}

      <input type="submit" />
    </form>
  );
}

function PersonForm({ lens }: { lens: Lens<{ name: string; surname: string }> }) {
  return (
    <>
      <StringInput lens={lens.focus('name')} />
      <StringInput lens={lens.focus('surname')} />
    </>
  );
}

function StringInput({ lens }: { lens: Lens<string> }) {
  return <input {...lens.interop((ctrl, name) => ctrl.register(name))} />;
}

All credits go to @aspirisen