简体   繁体   English

单击提交后无法自动清除输入字段(反应)

[英]Unable to clear input field automatically after clicking on submit (React)

I am new to react, and I am having trouble trying to make the input field clear automatically after clicking the submit button.我是新手,点击提交按钮后我无法自动清除输入字段。 I believe that I have made the component a fully controlled one by react, where the input value is solely dependent on the state's input.我相信我已经通过反应使组件成为一个完全受控的组件,其中输入值完全取决于状态的输入。 Hope someone is able to help.希望有人能够提供帮助。 Been stuck for a few hours now.现在被困了几个小时。 Really not sure where I've gone wrong.真的不确定我哪里出错了。 Thanks!谢谢!

import React, { Component } from 'react'; 
import './App.css';
import Axios from 'axios';
import { Link, BrowserRouter, Route} from 'react-router-dom';
import reviews from './Screen/Reviews'; 

class App extends Component{

  constructor() {
    super();

    this.state = {
      movieName: '',
      setReview: ''
    };

    this.onMovieChange = this.onMovieChange.bind(this);
    this.onReviewChange = this.onReviewChange.bind(this);
    this.submitReview = this.submitReview.bind(this);

  }
  onMovieChange(e) {
    this.setState({
      movieName: e.target.value
    });
  }
  onReviewChange(e) {
    this.setState({
      setReview: e.target.value
    });
  }
  submitReview(e) {
    e.preventDefault();
    const movieName = this.state.movieName;
    const movieReview = this.state.setReview;
    Axios.post('http://localhost:3001/api/insert',
    {movieName: movieName, movieReview: movieReview}).then(()=>
    {
      alert('Insert successfully');  
    }
    ) 
    this.setState({
      movieName: '',
      setReview: ''
    })
  }
    
  render() {
    return (
      <BrowserRouter>
      <div className="form">
      <form   >
        <ul className="form-container">
          <li>
            <h2>Movie Review</h2>
          </li>  
          <li>
            <label htmlFor="movieName">
            Movie Name {" "}
            </label>
            <input className="movieName" type="movieName" name="movieName" id="movieName" onChange={this.onMovieChange}>
            </input>
          </li>
          <li>
            <label htmlFor="review">Review{" "}</label>
            <input className="review" type="review" id="review" name="review" onChange={this.onReviewChange}>
            </input>
          </li>
          <li> 
          <button onClick={this.submitReview}  type="submit" className="button-register-registration ">Submit</button>

          </li>

          <li>
            <Link to="/review" className="button-register-for-sign-in" ><h4>Go to reviews</h4></Link>
          </li>
        </ul>
      </form>
      <main>
        <div className="content">
        <Route path={"/review"} component={reviews}></Route> 
        </div>
      </main>
      </div>
    </BrowserRouter>
    );
  }

}

you are treating input as an uncontrolled component, so react cannot control its value and hence not sync with its state. So make your input controlled by updating as below您将输入视为不受控制的组件,因此 React 无法控制其值,因此无法与其 state 同步。因此,通过如下更新来控制您的输入

  <input className="movieName" type="movieName" name="movieName" id="movieName" onChange={this.onMovieChange} value={this.state.movieName}>

Praveen Mathew Philip's answer is correct and should fix your problem, but there are other issues with your code. Praveen Mathew Philip 的回答是正确的,应该可以解决您的问题,但您的代码还有其他问题。

First, in your two inputs:首先,在您的两个输入中:

<li>
  <label htmlFor="movieName">
    Movie Name {" "}
  </label>
  <input className="movieName" type="movieName" name="movieName" id="movieName" onChange={this.onMovieChange}></input>
</li>
<li>
  <label htmlFor="review">Review{" "}</label>
  <input className="review" type="review" id="review" name="review" onChange={this.onReviewChange}></input>
</li>

There are no movieName and review types for input.没有用于输入的movieNamereview类型。 You are probably looking for a type="text" instead.您可能正在寻找type="text" For a full list of possible inputs, refer to this有关可能输入的完整列表,请参阅

You could get away with just one handleChange function that would get your input's name from the event and keep your code cleaner.您可以只使用一个handleChange function 来摆脱困境,它可以从事件中获取您输入的名称并使您的代码更简洁。

Something along the lines:沿线的东西:

handleChange(e) { this.setState({ [e.target.name]: e.target.value }) } 

Finally, you could improve your code by using a tool called Prettier.最后,您可以使用名为 Prettier 的工具来改进您的代码。

It would clean some indentation and replace some useless code such as它会清理一些缩进并替换一些无用的代码,例如

<Route path={"/review"} component={reviews}></Route> 

by经过

<Route path="/review" component={reviews} />

And so on...等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM