javascript - React JS Change text value and submit - Stack Overflow

Facing this Error : Failed propType: You provided a value prop to a form field without an onChange hand

Facing this Error : Failed propType: You provided a value prop to a form field without an onChange handler. This will render a read-only field. If the field should be mutable use defaultValue. Otherwise, set either onChange or readOnly. Check the render method of BasicInputBox.

I am returning Following Component :

<BasicInputBox label="Student Name :" valChange={this.nameChange} value={datafield.name}/>

and the ponent :

var BasicInputBox = React.createClass({
    render: function() {
    return <div>
        <tr>
        <td><label> { this.props.label } </label></td>
        <td><input type="text" onChange={ this.props.valChange } value={ this.props.value } /></td>
      </tr>
    </div>
    }
});

Value is not changing while changing in text field. And the following is the function of nameChange function

Facing this Error : Failed propType: You provided a value prop to a form field without an onChange handler. This will render a read-only field. If the field should be mutable use defaultValue. Otherwise, set either onChange or readOnly. Check the render method of BasicInputBox.

I am returning Following Component :

<BasicInputBox label="Student Name :" valChange={this.nameChange} value={datafield.name}/>

and the ponent :

var BasicInputBox = React.createClass({
    render: function() {
    return <div>
        <tr>
        <td><label> { this.props.label } </label></td>
        <td><input type="text" onChange={ this.props.valChange } value={ this.props.value } /></td>
      </tr>
    </div>
    }
});

Value is not changing while changing in text field. And the following is the function of nameChange function

Share Improve this question edited Dec 15, 2015 at 11:23 Jon Surrell 9,6578 gold badges51 silver badges55 bronze badges asked Dec 15, 2015 at 10:51 ParamParam 211 gold badge4 silver badges14 bronze badges 4
  • nameChange: function(event){ this.setState({name: event.target.value}) }, – Param Commented Dec 15, 2015 at 10:51
  • And can we assume that value will be correctly updated by a render call, and that nameChange will be changing a state somewhere that will cause a re-render? It would be helpful to see that code. – Jon Surrell Commented Dec 15, 2015 at 11:12
  • tr is not a legal child of div. Please show more code and make sure this is clean and working (up to the point where you're having trouble). – Jon Surrell Commented Dec 15, 2015 at 11:21
  • See spec: Contexts in which this element can be used. tr elements can't appear just anywhere. – Jon Surrell Commented Dec 15, 2015 at 11:24
Add a ment  | 

1 Answer 1

Reset to default 1

Your code appears to work fine, assuming the parts you have not shared are working correctly.

If you're having trouble, it is likely in the listener or in the render of the parent ponent. Here is a working example with your element (note: I have modified structure slightly to ensure legal html):

var BasicInputBox = React.createClass({
    render: function() {
    return <div>
        <label> { this.props.label } </label>
        <input type="text" onChange={ this.props.valChange } value={ this.props.value } />
    </div>
    }
});
var Wrapper = React.createClass({
  getInitialState: function() {
    return {
      datafield: {
        name: ''
      }      
    }
  },
  nameChange: function(e) {

    this.setState({datafield: {name: e.currentTarget.value}})

  },
  render: function() {
    let datafield = this.state.datafield
    return <BasicInputBox label="Student Name :" valChange={this.nameChange} value={datafield.name}/>
  }
})

Working snippet (ugly after babel/jsx translation).

"use strict";

var BasicInputBox = React.createClass({
  displayName: "BasicInputBox",

  render: function render() {
    return React.createElement(
      "div",
      null,
      React.createElement(
        "tr",
        null,
        React.createElement(
          "td",
          null,
          React.createElement(
            "label",
            null,
            " ",
            this.props.label,
            " "
          )
        ),
        React.createElement(
          "td",
          null,
          React.createElement("input", { type: "text", onChange: this.props.valChange, value: this.props.value })
        )
      )
    );
  }
});
var Wrapper = React.createClass({
  displayName: "Wrapper",

  getInitialState: function getInitialState() {
    return {
      datafield: {
        name: ''
      }
    };
  },
  nameChange: function nameChange(e) {
    this.setState({ datafield: { name: e.currentTarget.value } });
  },
  render: function render() {
    var datafield = this.state.datafield;
    return React.createElement(BasicInputBox, { label: "Student Name :", valChange: this.nameChange, value: datafield.name });
  }
});

ReactDOM.render(React.createElement(Wrapper, null), document.getElementById('app'));
<script src="https://fb.me/react-0.14.3.min.js"></script>
<script src="https://fb.me/react-dom-0.14.3.min.js"></script>
<div id="app"></div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745042818a4607913.html

相关推荐

  • javascript - React JS Change text value and submit - Stack Overflow

    Facing this Error : Failed propType: You provided a value prop to a form field without an onChange hand

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信