How did I manage to support IE 11 using RHF version 7 #7915
Unanswered
pooyarm
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Recently we adopted react-hook-form for our product, refactored many components in this regard but didn't expect RHF to not support IE 11 then had to fix the problem, going back to v6 wasn't an option for us. Here I want to explain how I managed to do that.
Transpile
Our FE is based on Next.js and for supporting browsers we're using corejs. Then I've updated our webpack configuration to transpile RHF with our babel configuration. With something like:
but it didn't work exactly as I expected, because the file RHF is exporting is named
index.esm.mjs
which is js module, and while our babel was trying to transpile it, for some reason it couldn't do it like the rest and thrown error. When I renamed that file manually toindex.esm.js
and changedpackage.json
accordingly, it worked properly so I was happy that the solution will work, I just needed to tell babel transpiler to considerindex.esm.mjs
like the rest js files. for that I've set module type tojavascript/auto
in our webpack configuration:What else?
After this everything worked fine on IE 11 except one error which was saying something like "contains is not a function". After I went deep in the bundled code I realised RHF is using
document.contains
which is not supported by IE 11, good thing is that it can be easily polyfilled by the following piece of code:I know the polyfill doesn't fill all cases, but still it's enough for our product. we don't have a case of asking document for something out of body.
Now everything works fine in IE 11 🎉
How RHF could help me to do it easier?
During the whole process I faced some obstacles that took much time to research and solve:
index.esm.mjs
, I think if RHF was exporting that file with.js
extension, it was easier to do it. but I don't complain!document.contains
, I thinkdocument.body.contains
could be used instead. (if I'm not mistaken)At the end I should say "Thank You ❤️ " to RHF team for such a great library.
Beta Was this translation helpful? Give feedback.
All reactions