-
Hi. i have some internal library where i created few components based on [email protected]. everything was ok when i build library in one js bundle. then i have split library by component (1 component = 1 file) and forms components broke. some part of my components.
Input:
|
Beta Was this translation helpful? Give feedback.
Replies: 17 comments 52 replies
-
make sure your |
Beta Was this translation helpful? Give feedback.
-
i have updated react-hook-form. but with 6.14.1 i have the same error( |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 hi) may i ask you to review it ;) |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 i solved this issue) |
Beta Was this translation helpful? Give feedback.
-
Stuck on this issue as well - using RHF 7.39.1, have added RHF to webpack externals, wrapped the component from my library in FormProvider but still useFormContext is null |
Beta Was this translation helpful? Give feedback.
-
Can we open as a issue for better tracking? |
Beta Was this translation helpful? Give feedback.
-
I think the easiest solution in this case is not to use the form context and apply a control inversion pattern. We need to pass the methods to the parent from the child, instead of getting them in the child via context. So we need something like: const Form = ({formMethods}) => {
return (
<form onSubmit={formMethods.handleSubmit(onSubmit)}>
{children}
<Button .../>
</form>
);
}; and const Input = (props) => {
const methods = useForm();
return (
<Form
formMethods={methods}
>
<CustomInput ... />
</Form>
)
} This works for us, I hope it helps. |
Beta Was this translation helpful? Give feedback.
-
Beware of the different package versions in case this happens to you in cross-package usage situation (as it did to me) |
Beta Was this translation helpful? Give feedback.
-
If you are using rollup for your library and webpack for your app, make sure your rollup output is in the es format.
|
Beta Was this translation helpful? Give feedback.
-
For anyone reading this who happens to be using a component library that uses react-hook-form like shadcn/ui make sure your Form component is being imported from the component library you're using (like |
Beta Was this translation helpful? Give feedback.
-
For future reference; I hade the same problem when creating a library that applies the context, when the projects using the library were Webpack Module Federated. It was not enough to only add new ModuleFederationPlugin({
shared: {
"react-hook-form": {
singleton: true,
version: "7.27.0"
}
// ...
},
// ...
}) Hope this can help someone |
Beta Was this translation helpful? Give feedback.
-
I'd like to share my take on this. Looks like as soon as you initiate your form with useForm, hooks from RHF library are inaccessible to children components. So I tried [sazzadtanim]s solution, but I'm not initiating props in children components before the return (all passed props or "methods" are run AFTER return). I'm actually running the prop directly and also making sure it runs only when it's accessible (props.getValues && props.getValues()). Looks like NextJS isn't following ReactJS loading patterns on build. Thank you sazzadtanim! |
Beta Was this translation helpful? Give feedback.
-
I also had this issue and found a simple workaround. Original issue:
Fix:
By simply adding a check to make sure that it isn't null, it got rid of the error. |
Beta Was this translation helpful? Give feedback.
-
const methods = useFormContext() ?? {}; const { fields, append, remove } = useFieldArray({ Doesn't seem to be working for me. |
Beta Was this translation helpful? Give feedback.
-
Based on @edwardnz2017 comment, I was able to solve the problem in a simpler way:
|
Beta Was this translation helpful? Give feedback.
-
If none of the other comments seem to match your situation, and you think everything in your code is correct and in right place, there's something I'd like you to check. It's pretty simple. If you render the function/const where you use All I did was change this part of my code;
to this:
|
Beta Was this translation helpful? Give feedback.
-
Another option I'd add is to have your React component to take interface Props {
// useFormContext does not work when used across packages
// We need to get this from the caller instead
useFormContext?: typeof useFormContext;
}
const YourComponent: React.FC<Props> = ({
useFormContext: useFormContextFunc = useFormContext,
}) => {
const {
watch,
register,
formState: { errors },
} = useFormContextFunc();
...
}; This way, you can still test your component within NPM package by using |
Beta Was this translation helpful? Give feedback.
@bluebill1049 i solved this issue)
basically what i need it is just adding react-hook-form as external library in webpack config
and now, csb is working :)