`e.preventDefault()` 在 react-hook-form 中无法正常工作。

huangapple go评论116阅读模式
英文:

e.preventdefault() isn't working with react-hook-form

问题

我正在尝试在React中使用react-hook-form提交表单后禁用页面重新加载,然而即使使用了e.preventDefault()也会重新加载页面。我尝试在官方文档中找到解决方法,但他们只是建议使用e参数。

如何解决这个问题?我已经尝试传递事件但不起作用。

英文:

I'm trying to disable the page reload after submit a form in react using react-hook-form, however even using the e.preventDefault() the page reload. I tried to found something in the official docs but they just say to use the e param

  1. import { useRef } from "react"
  2. import { useForm } from "react-hook-form"
  3. function NewItemModal() {
  4. const { register, handleSubmit } = useForm()
  5. function handleAddNewItem(data: Object, e?: Event) {
  6. e?.preventDefault()
  7. console.log(data)
  8. }
  9. return (
  10. <>
  11. <form onSubmit={() => handleSubmit((e) => handleAddNewItem(e))} className="space-y-8">
  12. <div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-2 place-items-center gap-y-6'>
  13. <label className='space-y-1'>
  14. <h4>ID:</h4>
  15. <motion.input whileFocus={{ borderColor: '#FF9400' }} type="text" className='border border-raisin rounded indent-1 outline-none'
  16. {...register('item_id')} />
  17. </label>
  18. <div></div>
  19. <label className='space-y-1'>
  20. <h4>Nome:</h4>
  21. <motion.input whileFocus={{ borderColor: '#FF9400' }} type="text" className='border border-raisin rounded indent-1 outline-none'
  22. {...register('name')} />
  23. </label>
  24. <label className='space-y-1'>
  25. <h4>Condição:</h4>
  26. <motion.input whileFocus={{ borderColor: '#FF9400' }} type="text" className='border border-raisin rounded indent-1 outline-none'
  27. {...register('condition')} />
  28. </label>
  29. <label className='space-y-1'>
  30. <h4>Marca:</h4>
  31. <motion.input whileFocus={{ borderColor: '#FF9400' }} type="text" className='border border-raisin rounded indent-1 outline-none'
  32. {...register('brand')} />
  33. </label>
  34. <label className='space-y-1'>
  35. <h4>Setor:</h4>
  36. <motion.input whileFocus={{ borderColor: '#FF9400' }} type="text" className='border border-raisin rounded indent-1 outline-none'
  37. {...register('sector')} />
  38. </label>
  39. <label className='space-y-1'>
  40. <h4>Comodatário:</h4>
  41. <motion.input whileFocus={{ borderColor: '#FF9400' }} type="text" className='border border-raisin rounded indent-1 outline-none'
  42. {...register('borrower')} />
  43. </label>
  44. <label className='space-y-1'>
  45. <h4>Data:</h4>
  46. <motion.input whileFocus={{ borderColor: '#FF9400' }} type="text" className='border border-raisin rounded indent-1 outline-none'
  47. {...register('borrowerDate')} />
  48. </label>
  49. </div>
  50. <div className='px-4 py-1 space-y-6 flex flex-col'>
  51. <label className='space-y-3 flex flex-col items-center'>
  52. <h4 className="self-start">Descrição:</h4>
  53. <motion.textarea whileFocus={{ borderColor: '#FF9400' }} className='p-2 w-full h-56 outline-none border border-raisin rounded text-sm'></motion.textarea>
  54. </label>
  55. <div className='flex justify-end'>
  56. <motion.button whileHover={{ scale: 1.03 }} type="submit" className="bg-princeton px-2 w-20 h-8 rounded border-2 border-raisin">
  57. Enviar
  58. </motion.button>
  59. </div>
  60. </div>
  61. </form>
  62. </>
  63. )
  64. }
  65. export default NewItemModal

How can i resolve this? I already tried to pass the event but it doesn't work

答案1

得分: 2

You should directly pass the return value of handleSubmit to onSubmit, without wrapping it in another arrow function. There is then no need to call event.preventDefault() manually.

  1. <form onSubmit={handleSubmit(handleAddNewItem)}>
英文:

You should directly pass the return value of handleSubmit to onSubmit, without wrapping it in another arrow function. There is then no need to call event.preventDefault() manually.

  1. &lt;form onSubmit={handleSubmit(handleAddNewItem)}&gt;

huangapple
  • 本文由 发表于 2023年8月4日 22:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定