Core Concepts: How Lenses Work
mainLenses in this library act as carriers for control and name information. They are designed to describe form structure and provide type-safe paths to nested data, rather than acting as containers for the data itself.
Key Principles
No Setters and Getters: The library does not provide methods to get or set values directly. This prevents bugs where developers might read a value from a lens expecting reactivity, which is not how React Hook Form operates. Always use standard React Hook Form hooks (like
useWatch) to access reactive values.Do Not Operate Values Directly: Avoid consuming raw values from the form state directly within components that rely on lenses. Because these values are not inherently reactive through the lens itself, components may fail to re-render. Instead, consume values via appropriate RHF hooks and pass them down as props.
Interop Pattern: Use
.interop()to convert a lens into a format compatible with React Hook Form's internal APIs. For example, when usingregister, you can pass a callback tointeropthat receives the controller and the field name:lens.interop((ctrl, name) => ctrl.register(name)).