React TypeScript onSubmit e.preventDefault() not working
Asked Answered
M

5

16

I'm building a simple user search app using React and TypeScript. I have a basic form with an input text box to search users and an input button that submits the search. But the e.preventDefault() method in my onSubmit method doesn't seem to be working. When I submit, the whole app refreshes. Actually, it may be that onSubmit isn't even being called at all.

  private handleSubmit = (e: React.FormEvent<HTMLInputElement>) => {
    e.preventDefault();
    this.props.searchUsers(this.state.text);
    this.setState({text: ''});
  }

  private handleChange = (e: React.FormEvent<HTMLInputElement>) => {
    this.setState({text: e.currentTarget.value});
  }

  public render() {
    return (
      <div>
        <form className="form">
          <input 
            type="text" 
            name="text" 
            value={this.state.text} 
            placeholder="Search Users..."
            onChange={this.handleChange}
          />
          <input 
            type="submit"
            value="search"
            className="btn btn-dark btn-block" 
            onSubmit={this.handleSubmit}
          />
        </form>
      </div>
    );
  }

Any suggestions? Thanks!

Mohun answered 12/6, 2019 at 12:22 Comment(1)
This should help you out: medium.com/@ericclemmons/…Inflexion
S
23

You are adding onSubmitin the wrong place.

You should add in the form not in the button.

  public render() {
    return (
      <div>                    // \/ added here
        <form className="form" onSubmit={this.handleSubmit}>
          <input 
            type="text" 
            name="text" 
            value={this.state.text} 
            placeholder="Search Users..."
            onChange={this.handleChange}
          />
          <input                 // removed from the button
            type="submit"
            value="search"
            className="btn btn-dark btn-block" 
          />
        </form>
      </div>
    );
  }

If you look at the example on the docs you will see that they add it in the form.

*Remember that to trigger the form's onSubmit, your input/button needs to have type="submit"

Servility answered 12/6, 2019 at 12:27 Comment(0)
G
13

You can simply do this :-

 private handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    this.props.searchUsers(this.state.text);
    this.setState({text: ''});
  }

  private handleChange = (e: React.FormEvent<HTMLInputElement>) => {
    this.setState({text: e.currentTarget.value});
  }

  public render() {
    return (
      <div>
        <form className="form"   onSubmit={(e) =>this.handleSubmit(e)}>
          <input 
            type="text" 
            name="text" 
            value={this.state.text} 
            placeholder="Search Users..."
            onChange={this.handleChange}
          />
          <input 
            type="submit"
            value="search"
            className="btn btn-dark btn-block" 
          
          />
        </form>
      </div>
    );
  }
Glottalized answered 16/6, 2021 at 19:45 Comment(0)
C
8

Also in addition to moving the event from onClick to onSubmit, to get the event from the form, type React.FormEvent works.

  private handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    this.props.searchUsers(this.state.text);
    this.setState({text: ''});
  }```

Instead of e:React.FormEvent<HTMLInputElement> 
Chesterton answered 7/6, 2021 at 20:13 Comment(0)
I
3

I fixed the similar issue by removing e.preventDefault from handleSubmit function and adding **onClick={e => {e.preventDefault(); this.handleSubmit()}}** on button. and you have to put onSubmit on Form.

  private handleSubmit = (e: React.FormEvent<HTMLInputElement>) => {
    this.props.searchUsers(this.state.text);
    this.setState({text: ''});
  }
  public render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit} className="form">
          <input 
            type="text" 
            name="text" 
            value={this.state.text} 
            placeholder="Search Users..."
            onChange={this.handleChange}
          />
          <input 
            type="submit"
            value="search"
            className="btn btn-dark btn-block" 
            onClick={e => {e.preventDefault(); this.handleSubmit()}}
          />
        </form>
      </div>
    );
  }

This is tested and should work.

Inspan answered 8/1, 2020 at 16:53 Comment(1)
The form submit handler should have an event type of: e: React.FormEvent<HTMLFormElement>Ornate
S
0
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault()
    }

return (
    <form onSubmit={e => handleSubmit(e)}>
            <label>Name</label>
            <input type="text" placeholder='John Doe'/>

            <label>Email</label>
            <input type="email" placeholder='[email protected]'/>

            <input type="submit"/>
    </form>
)
Subarctic answered 19/4, 2023 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.