javascript - ReactStrap Handle Input Implicit Submit - Stack Overflow

Whenever I press Enter when focused on the Input text box, the implicit submit from the Input element t

Whenever I press Enter when focused on the Input text box, the implicit submit from the Input element triggers a submission and reloads the page:

import React, { Component } from "react";
import { Col, Button, Form, FormGroup, Label, Input } from "reactstrap";
import "./SearchBar.css";

class SearchBar extends Component {
constructor(props) {
    super(props);
    this.state = {
      term: ""
    };

    this.handleTermChange = this.handleTermChange.bind(this);
    this.handleSearch = this.handleSearch.bind(this);
    this.handleEnter = this.handleEnter.bind(this);
}

handleTermChange(e) {
    this.setState({ term: e.target.value });
}

handleSearch() {
    this.props.searchEngine(this.state.term);
}

handleEnter(e) {
    if (e.key === 13) {
      this.handleSearch();
    }
}

render() {
    return (
    <Form className="searchbox">
        <FormGroup row>
        <Label for="searchId" sm={2}>
            Search Engine
        </Label>
        <Col sm={10}>
            <Input
            type="text"
            placeholder="Enter Sth"
            onChange={this.handleTermChange}
            onKeyDown={this.handleEnter}
            />
        </Col>
        </FormGroup>

        <FormGroup>
        <Col sm="2">
            <div className="">
            <Button
                onClick={this.handleSearch}
                className="btn"
            >
                Submit
            </Button>
            </div>
        </Col>
        </FormGroup>
    </Form>
    );
}
}

export default SearchBar;

I just want to trigger the search function with the handler as above, but avoid the implicit submit, i.e. same function when clicking on the Submit button. Otherwise it's just reloading the page and wash out the returned result.

What did I do wrong? I didn't experience this issue with basically the same pattern before.

Dependencies:

  • "reactstrap": "^6.5.0"
  • "bootstrap": "^4.1.3"
  • "react": "^16.6.3"
  • "react-dom": "^16.6.3"
  • "react-scripts": "2.1.1"

Whenever I press Enter when focused on the Input text box, the implicit submit from the Input element triggers a submission and reloads the page:

import React, { Component } from "react";
import { Col, Button, Form, FormGroup, Label, Input } from "reactstrap";
import "./SearchBar.css";

class SearchBar extends Component {
constructor(props) {
    super(props);
    this.state = {
      term: ""
    };

    this.handleTermChange = this.handleTermChange.bind(this);
    this.handleSearch = this.handleSearch.bind(this);
    this.handleEnter = this.handleEnter.bind(this);
}

handleTermChange(e) {
    this.setState({ term: e.target.value });
}

handleSearch() {
    this.props.searchEngine(this.state.term);
}

handleEnter(e) {
    if (e.key === 13) {
      this.handleSearch();
    }
}

render() {
    return (
    <Form className="searchbox">
        <FormGroup row>
        <Label for="searchId" sm={2}>
            Search Engine
        </Label>
        <Col sm={10}>
            <Input
            type="text"
            placeholder="Enter Sth"
            onChange={this.handleTermChange}
            onKeyDown={this.handleEnter}
            />
        </Col>
        </FormGroup>

        <FormGroup>
        <Col sm="2">
            <div className="">
            <Button
                onClick={this.handleSearch}
                className="btn"
            >
                Submit
            </Button>
            </div>
        </Col>
        </FormGroup>
    </Form>
    );
}
}

export default SearchBar;

I just want to trigger the search function with the handler as above, but avoid the implicit submit, i.e. same function when clicking on the Submit button. Otherwise it's just reloading the page and wash out the returned result.

What did I do wrong? I didn't experience this issue with basically the same pattern before.

Dependencies:

  • "reactstrap": "^6.5.0"
  • "bootstrap": "^4.1.3"
  • "react": "^16.6.3"
  • "react-dom": "^16.6.3"
  • "react-scripts": "2.1.1"
Share Improve this question asked Dec 12, 2018 at 15:56 suvtfopwsuvtfopw 1,03811 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I found out that it is the <Form> element that triggers the implicit submission. I change it to <Form className="searchbox" onSubmit={this.handleSubmit}> and add a new function:

handleSubmit(e) { 
    e.preventDefault(); 
    this.handleSearch();
}

Full working code modified from question:

import React, { Component } from "react";
import { Col, Button, Form, FormGroup, Label, Input } from "reactstrap";
import "./SearchBar.css";

class SearchBar extends Component {
constructor(props) {
    super(props);
        this.state = {
            term: ""
        };

    this.handleTermChange = this.handleTermChange.bind(this);
    this.handleSearch = this.handleSearch.bind(this);
    this.handleEnter = this.handleEnter.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleTermChange(e) {
        this.setState({ term: e.target.value });
    }

    handleSearch() {
        this.props.searchEngine(this.state.term);
    }

    handleEnter(e) {
        if (e.key === 13) {
        this.handleSearch();
        }
    }

    handleSubmit(e) {
        e.preventDefault();
        this.handleSearch();
   }

render() {
    return (
    <Form className="searchbox" onSubmit={this.handleSubmit}>
        <FormGroup row>
        <Label for="searchId" sm={2}>
            Search Engine
        </Label>
        <Col sm={10}>
            <Input
            type="text"
            placeholder="Enter Sth"
            onChange={this.handleTermChange}
            onKeyDown={this.handleEnter}
            />
        </Col>
        </FormGroup>

        <FormGroup>
        <Col sm="2">
            <div className="">
            <Button
                onClick={this.handleSearch}
                className="btn"
            >
                Submit
            </Button>
            </div>
        </Col>
        </FormGroup>
    </Form>
    );
}
}

export default SearchBar;

You need to prevent default event when Enter key pressed.

handleEnter(e) {
    if (e.key === 13) {
     e.preventDefault();
     this.handleSearch();
    }
}

e.preventDefault() tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744852937a4597237.html

相关推荐

  • javascript - ReactStrap Handle Input Implicit Submit - Stack Overflow

    Whenever I press Enter when focused on the Input text box, the implicit submit from the Input element t

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信