【问题标题】:React warning: "is changing an uncontrolled input of type hidden to be controlled" why?反应警告:“正在更改隐藏类型的不受控制的输入以进行控制”为什么?
【发布时间】:2017-07-26 11:48:06
【问题描述】:

我不知道如何解决此警告。这是发送警告的组件:

警告:ReporteBoxMapRaster 正在将隐藏类型的不受控输入更改为受控。输入元素不应从不受控切换到受控(反之亦然)。在组件的生命周期内决定使用受控输入元素还是不受控输入元素。更多信息:htt fb sitereact-controlled-components

var ReporteBoxMapRaster = React.createClass({
    print: function(){
        $(ReactDOM.findDOMNode(this.refs.print_form)).submit();
    },

    render: function() {
        if( this.props.extent ){
            var legend = null;
            switch( this.props.legend ){
                case 'solid':
                    legend = <ReporteLegendSolid values={ this.props.values } />;
                break;
                case 'gradient':
                    legend = <ReporteLegendGradient values={ this.props.values } />;
                break;
            }

            if( this.props.extra )
                var extra = this.props.extra;
            else
                var extra = '';

            var extent = this.props.extent;
            var url = 'http://XXX.XXX.XXX/cgi-bin/mapserv?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image/png&TRANSPARENT=true&';
            url += 'LAYERS='+ this.props.layer +'&';
            url += 'map=/opt/mapfile/reporte/'+ this.props.mapfile +'.map&';
            url += 'TILED=false&WIDTH=780&HEIGHT=440&STYLES=&';
            url += 'CRS=EPSG:' + this.props.epsg + '&';
            url += 'BBOX=' + extent[0] + ',' + extent[1] + ',' + extent[2] + ',' + extent[3];
            url += extra;

            return (
                <div className="uk-width-large-2-2 reporte-box reporte-box-target" >
                    <div className="md-card md-card-hover md-card-overlay " >
                        <div className="md-card-toolbar full2">
                            <div className="md-card-toolbar-actions">
                                <Full_screem_inter />
                                <span onClick={this.print} ><i className="md-icon material-icons md-card-overlay-toggler" >print</i></span>
                            </div>
                            <h3 className="md-card-toolbar-heading-text">{ this.props.title }</h3>
                        </div>
                        <div className="md-card-content uk-text-center" >
                            <div className="" >
                                <div className="map-container" >
                                    <img className="mapimage" ref="mapImageBG" src={ this.props.bgimage } />
                                    <img className="mapimage" ref="mapimageWMS" src={ url } />
                                </div>
                            </div>

                            <div className="reporte-onlyonfull">
                                Leyenda
                            </div>
                            <div>
                                { legend }
                            </div>

                            <div className="reporte-onlyonfull">
                                <div>
                                </div>
                            </div>
                        </div>
                        <div className="md-card-footer" ></div>
                    </div>

                    <form ref="print_form" action={ Routing.generate("exportmaptopdf") } method="post" target="_blank" className="hidden" >
                        <input name="legend" type="hidden" value={JSON.stringify( this.props.values )} />
                        <input name="legendType" type="hidden" value={this.props.legend} />
                        <input name="title" type="hidden" value={this.props.title} />
                        <input name="bgsrc" type="hidden" value={this.props.bgimage} />
                        <input name="src" type="hidden" value={ encodeURI( url ) } />
                    </form>
                </div>
            );
        }
        else
            return (<div></div>);
    }
});
module.exports = ReporteBoxMapRaster;

【问题讨论】:

  • 将输入中的值更改为 defaultValue。 docs
  • 嗯,是的,现在我没有该组件的警告,但另一个有同样的问题,还有更多。将 value 更改为 defaultValue 会有副作用吗?
  • 显示另一个错误。

标签: reactjs


【解决方案1】:

您可以在该代码 sn-p 中改进很多东西;)但回答您的问题:

每当您为输入设置undefined 值时,您都会收到该警告。为了消除警告,避免设置undefined,而是设置一个空字符串''

class Inputs extends React.Component {

  componentWillMount() {
    this.setState({
      // value: '', // <---- this doesn't show the warning!!
      value: undefined, // <---- Undefined value, you get the warning :o
    });
  }

  setValue(event) {
    this.setState({
      value: event.target.value,
    });
  }

  render() {
    return (
      <div>
        <input
          type="text"
          onChange={(event) => this.setValue(event)}
          value={this.state.value}
        />
      </div>
    );
  }
}

这是一个工作示例:http://jsbin.com/sizaxaf/1/edit?html,js,output

在您的情况下,this.props.title 或任何其他道具的值未定义,因此您会收到警告。当 prop 未定义(例如空字符串)时,您可以使用 defaultProps 设置值。

class Inputs extends React.Component {
  static defaultProps = {
    title: '',
  }

  //...
}

【讨论】:

  • 谢谢,不知道是 undefined 造成的。
猜你喜欢
  • 2020-08-06
  • 2018-01-13
  • 2023-03-05
  • 2017-09-23
  • 2018-11-30
  • 2017-10-21
  • 2019-04-17
  • 1970-01-01
相关资源
最近更新 更多