Skip to content Skip to sidebar Skip to footer

How To Prevent Page Reload Using Form.submit() To Submit As A Method In Reactjs

How to prevent page refreshing on firing form submission programmatically in ReactJS? I've tried this code: const myForm = () =>
{ if(!e.rel

Solution 1:

Use should use e.currentTarget.requestSubmit() (docs). Read about differences with submit method here.

Or you may simply define a submit handler function and provide it to both onBlur and onSubmit form properties.

consthandleSubmit = (ev) => {
  ev.preventDefault()
  ... 
}

return (
  <formonBlur={(ev) => if (shouldSubmit(ev)) handleSubmit(ev)}
    onSubmit={handleSubmit}
  >
  ...
  </form>
)

Without resorting to native form submission. If your use case allows that.

Post a Comment for "How To Prevent Page Reload Using Form.submit() To Submit As A Method In Reactjs"