【问题标题】:Clear input field after button clicked单击按钮后清除输入字段
【发布时间】:2018-11-21 15:06:30
【问题描述】:

我正在构建一个简单的任务列表,并在按下按钮后坚持清除输入字段,我们将不胜感激。我已经使用 react 扩展对其进行了检查,它正在将值添加到我想要的列表中并保留 curVal,但输入框保持不变

class App extends Component {
  state = {
    taskList: [],
    curValue: ""
  };

  changeHandler = event => {
    this.setState({ curValue: event.target.value });
  };

  newTaskHandler = () => {
    const itemValue = this.state.curValue;
    const clearInput = "";
    this.setState({
      taskList: [...this.state.taskList, itemValue],
      curValue: ""
    });
  };

  render() {
    return (
      <div className="App">
        <h1>Task List</h1>
        <TaskInput
          click={this.newTaskHandler}
          value={this.state.curValue}
          changed={this.changeHandler}
        />

        <h1>{this.curValue}</h1>
        <h1>{this.taskList}</h1>
      </div>
    );
  }
}

const TaskInput = props => {
  return (
    <div>
      <input
        type="text"
        name="task"
        value={props.curValue}
        onChange={props.changed}
      />
      <button type="submit" onClick={props.click}>
        Add to your Task List{" "}
      </button>
    </div>
  );
};

【问题讨论】:

    标签: reactjs forms


    【解决方案1】:

    你给TaskInput 的道具被命名为value,而不是curValue

    props.curValue 更改为props.value,它将按预期工作。

    const TaskInput = props => {
      return (
        <div>
          <input
            type="text"
            name="task"
            value={props.value}
            onChange={props.changed}
          />
          <button type="submit" onClick={props.click}>
            Add to your Task List
          </button>
        </div>
      );
    };
    

    class App extends React.Component {
      state = {
        taskList: [],
        curValue: ""
      };
    
      changeHandler = event => {
        this.setState({ curValue: event.target.value });
      };
    
      newTaskHandler = () => {
        const itemValue = this.state.curValue;
        const clearInput = "";
        this.setState({
          taskList: [...this.state.taskList, itemValue],
          curValue: ""
        });
      };
    
      render() {
        return (
          <div className="App">
            <h1>Task List</h1>
            <TaskInput
              click={this.newTaskHandler}
              value={this.state.curValue}
              changed={this.changeHandler}
            />
    
            <h1>{this.curValue}</h1>
            <h1>{this.taskList}</h1>
          </div>
        );
      }
    }
    
    const TaskInput = props => {
      return (
        <div>
          <input
            type="text"
            name="task"
            value={props.value}
            onChange={props.changed}
          />
          <button type="submit" onClick={props.click}>
            Add to your Task List
          </button>
        </div>
      );
    };
    
    ReactDOM.render(<App />, document.getElementById("root"));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    
    <div id="root"></div>

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    相关资源
    最近更新 更多