【问题标题】:React: Element is changing a controlled input of type text to be uncontrolled反应:元素正在将文本类型的受控输入更改为不受控制
【发布时间】:2017-04-21 03:35:12
【问题描述】:

我收到以下错误,但无法确定它发生的原因

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

我有以下文件:

  1. Searchbox.js

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

还有 App.js 文件

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;

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

有什么建议吗?

【问题讨论】:

  • 它对我来说工作正常。
  • 刚刚将您的代码粘贴到 codepen 中,没有任何警告或错误

标签: javascript reactjs event-handling react-dom


【解决方案1】:

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

中获取值

onSearchChange 替换为以下代码:

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

【讨论】:

  • 来吧!我不敢相信自己!我一直在努力寻找这个问题! React 的警告信息应该更有帮助!非常感谢里特什!!接得好! (y)
  • 当传递给输入的值从未定义更改为字符串值或反之亦然时,通常会出现此警告。
  • 如果值被 prop 更新了怎么办?我有一个子组件,它的值正在由父组件通过道具更新,我收到了这个确切的错误
  • 常量值 = this.props.value || '' 。将此值传递给输入组件。
【解决方案2】:

我收到了同样的警告。我的 TextFieldItem 是一个子组件,正在通过其道具进行更新。以下行是罪魁祸首。

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;

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 2020-08-06
    • 2018-01-13
    • 1970-01-01
    • 2023-03-05
    • 2017-07-25
    • 2017-09-23
    • 1970-01-01
    • 2021-05-21
    相关资源
    最近更新 更多