Replies: 2 comments
-
I am also trying to make reusable parts of nested forms type-safe. Have been struggling with passing in type Foo = {
bar: string:
}
type Props<T extends Foo> = {
control: Control<T>;
}
function Component<T extends Foo>({ control }) {
const bar = useWatch({control, name: "bar" }) // types aren't working
} And this example doesn't even touch the different name prefixes, which make situation even worse. So yeah, I am also considering building something similar to your solution right now, thanks for a good starting point 😜. I could offer you some suggestions based on what I see here:
As for the drawbacks, I am not sure, seems like a good solution for a complex problem, but you would be creating a new const defaultPropsAreEqual = /** react default memo compareFn impl. here */
const formMemo = <TProps extends { frame: Frame }>(
render: (props: TProps) => React.ReactNode,
propsAreEqual?: (
prevProps: Readonly<Omit<TProps, 'frame'>>,
nextProps: Readonly<Omit<TProps, 'frame'>>
) => boolean
): (props: TProps) => React.ReactNode => {
return memo(render, ({frame: prevFrame, ...prev}, {frame: nextFrame, ...next}) => {
if (prevFrame.path !== nextFrame.path) {
return false
}
return propsAreEqual ? propsAreEqual(prev, next) : defaultPropsAreEqual(prev, next)
})
}; For simple applications without the need for nested, reusable forms, I would recommend passing |
Beta Was this translation helpful? Give feedback.
-
Also, there is a PR for lens solution, which is very similar to yours #12284 , it uses cache object (Map) so that it doesn't require any additional memoization once a |
Beta Was this translation helpful? Give feedback.
-
Hey team, I am using rhf heavily at work and we are building up a pretty large set of reusable and deep components for our application. In doing so we are finding the typesafety story, more than 1 level deep, to be kind of lacking. Before, we were passing around name prefixes and hoping the corresponding schema matches up deep in the tree.
But recently I thought of attaching a type to this value, calling it a Frame. Where the
to
method below traverses through refining the type. then when the schema or component changes you get a ts error.I have also attached some other helper functions to make adding new components easier...
Please let me know if this seems like a good idea, given the implementation of rhf. Also if I am missing an easier solution, any advice would be greatly appreciated. Thanks for making a great library!
Beta Was this translation helpful? Give feedback.
All reactions