You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can extend the basic lens functionality by adding custom methods to the `LensBase` interface. This is useful when you need additional methods that aren't available in the default lens API.
802
+
803
+
For example, let's add a `getValue` method to the lens that allows you to easily retrieve the current form values.
804
+
805
+
**Step 1: Create the type declarations file**
806
+
807
+
Create a `lenses.d.ts` file to extend the basic interface with the methods you want:
808
+
809
+
```typescript
810
+
declaremodule"@hookform/lenses" {
811
+
interfaceLensBase<T> {
812
+
getValue():T
813
+
}
814
+
}
815
+
816
+
export {}
817
+
```
818
+
819
+
**Step 2: Create the custom lens core implementation**
820
+
821
+
Create a `MyLensCore.ts` file with the actual runtime implementation:
Now you can use this hook as usual and you have the new method with correct TypeScript support:
865
+
866
+
```typescript
867
+
const lens =useMyLens(form)
868
+
lens.getValue() // Your custom method is now available with full type support
869
+
```
870
+
871
+
This pattern allows you to add any custom functionality to lenses while maintaining full type safety and compatibility with the existing lens API.
872
+
801
873
<Admonitiontype="tip"title="Questions or Feedback?">
802
874
803
875
Found a bug or have a feature request? Check out the [GitHub repository](https://github.com/react-hook-form/lenses) to report issues or contribute to the project.
0 commit comments