反应警告:“正在改变隐藏类型的不受控制的输入以进行控制”为什么?

时间:2017-03-06 14:45:17

标签: reactjs

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

警告:ReporteBoxMapRaster正在更改要控制的hidden类型的非受控输入。输入元素不应从不受控制切换到受控制(或反之亦然)。决定在组件的使用寿命期间使用受控或不受控制的输入元件。更多信息: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;

1 个答案:

答案 0 :(得分:7)

您可以在该代码段中改进很多内容;)但回答您的问题:

每当您为输入设置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或任何其他道具值未定义,因此您会收到警告。您可以使用defaultProps在未定义道具时设置值(例如,空字符串)。

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

  //...
}