【问题标题】:How come I can't type anything into my React search box?为什么我不能在我的 React 搜索框中输入任何内容?
【发布时间】:2017-05-02 15:36:40
【问题描述】:

我有一个名为 SearchInput 的组件,其中包含

  <input
    type='text'
    className='input-field'
    value={value}
    placeholder={this.state.placeholder}
    // onFocus={::this.onFocus}
    // onBlur={::this.onBlur}
    // onKeyUp={::this.onKeyUp}
    // onKeyDown={::this.hanleArrowKeys}
    // onChange={::this.onChange}
  />

但是,当我在其中输入任何内容时,什么也没有发生。文字甚至没有出现。我做错了什么?

【问题讨论】:

    标签: reactjs react-jsx


    【解决方案1】:

    可能你的状态没有更新,也许你没有将“this”绑定到onChange函数?

    以下是 react 中正确输入的示例:

    class SearchInput extends Component {
      constructor(props) {
        super(props);
        this.state = { value: '' };
        this.handleInputChange = this.handleInputChange.bind(this);
      }
    
      handleInputChange(event) {
        this.setState({ value: event.target.value });
      }
    
      render() {
        return <input type="text" value={this.state.value} onChange={this.handleInputChange}/>;
      }
    }
    

    【讨论】:

      【解决方案2】:

      可能您正在将输入框的值与组件状态中的属性绑定,并且您没有为输入标记提供 onChange 道具,或者您的 onChange 回调方法没有更新状态中的属性输入标签的值是绑定的。

      【讨论】:

      猜你喜欢
      • 2016-11-18
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多