React:元素正在更改文本类型的受控输入,使其不受控制

时间:2017-04-21 03:35:13

标签: javascript reactjs event-handling react-dom

我收到了以下错误,但未能确定原因发生的原因

warning.js:36警告:Searchbox正在更改类型文本的受控输入,使其不受控制。输入元素不应从受控切换到不受控制(反之亦然)。决定在组件的生命周期内使用受控或不受控制的输入元素

我有以下文件:

  1. Searchbox.js
  2. 
    
    import React, { Component } from 'react';
        
        class Searchbox extends Component {
        
          // render method
          render() {
            const { value, onChange, onSubmit, children } = this.props
            console.log(value)
            console.log(onChange)
            console.log(onSubmit)
            console.log(children)
            return (
              <section className="search-section">
                <form onSubmit={onSubmit}>
                  <input
                    type="text"
                    className="search-box"
                    value={value}
                    onChange={onChange}
                  />
                  <button type="submit">{children}</button>
                </form>
              </section>
            )
          }
        }
        
        export default Searchbox
    &#13;
    &#13;
    &#13;

    和App.js文件

    &#13;
    &#13;
    import React, { Component } from 'react';
    import Searchbox from './components/Searchbox'
    //import logo from './logo.svg';
    import './App.css';
    
    const DEFAULT_TERM = 'High Fidelity'
    
    class App extends Component {
    
      // Constructor
      constructor(props) {
        super(props)
    
        this.state = {
          movie: null,
          searchTerm: DEFAULT_TERM
        }
    
        this.onSearchSubmit = this.onSearchSubmit.bind(this)
        this.onSearchChange = this.onSearchChange.bind(this)
      }
    
    
      // onSearchSubmit method
      onSearchSubmit(e) {
        e.preventDefault()
        console.log("Searching movie with name" + this.status.searchTerm)
      }
    
      onSearchChange(e){
        console.log(event.target.value)
        this.setState({ searchTerm: event.target.value })
      }
    
      // render method
      render() {
    
        const { movie, searchTerm } = this.state
    
        return (
          <div className="App">
            <Searchbox
              value={searchTerm}
              onChange={this.onSearchChange}
              onSubmit={this.onSearchSubmit}
            >Search
            </Searchbox>
          </div>
        );
      }
    }
    
    export default App;
    &#13;
    &#13;
    &#13;

    加载一切都很好,但是当我在文本字段中输入内容时会触发错误。

    有什么建议吗?

2 个答案:

答案 0 :(得分:4)

问题出在 onSearchChange 函数中。您已将输入的onChange事件命名为 e ,但您从全局变量事件中获取值

onSearchChange 替换为下面提到的代码:

onSearchChange(event) {
   this.setState({ searchTerm: event.target.value })
}

答案 1 :(得分:1)

我收到了同样的确切警告。我的TextFieldItem是一个通过其props更新的子组件。以下行是罪魁祸首。

const fieldValue = (field.Value == null) ? undefined : field.Value;

更改行以将值从undefined设置为''已解决的警告。

const fieldValue = (field.Value == null) ? '' : field.Value;

反应组件

class TextFieldItem extends React.Component{
        constructor(props){
            super(props);
            this.onChange=this.onChange.bind(this);
        }
        onChange(e){
            event.preventDefault();
            const {onFieldChange,field} = this.props;   
            onFieldChange(e.target.id, e.target.value, e.target.name,field.Type);
        }
        render(){        
            const {field, onFieldChange, onRecordUpdate, IsLookupField,record,currentDocumentType} = this.props;       
            const fieldValue = (field.Value == null) ? '' : field.Value;
            return (
                <div>
                    <label>{field.Name}</label> 
                    <input type="text" key={field.Id} className="form-control" id={field.Id} name={field.Name} onChange={this.onChange} value={fieldValue} />     
                    {IsLookupField && <LookupFieldItem record={record} field={field} onRecordUpdate={onRecordUpdate} documentType={currentDocumentType} /> }
                </div>
                );
        }
    }
         export default TextFieldItem;