Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React re-render changes <input> value field first before type and re-rendering the node #6663

Closed
tisghans opened this issue Apr 30, 2016 · 1 comment

Comments

@tisghans
Copy link

tisghans commented Apr 30, 2016

The following code generates two <button>'s and an <input> field whose type changes respective to the type of button clicked (or dependent on the component state).

So, if my rendered field is type number and it has the value 42 and I tap the <button> to change the field type to text with new value hello world!, react tries to set <input type='number' />'s value to 'hello world!' before changing field type to text, which generates a warning in the console as show below:
screen shot 2016-04-29 at 9 06 45 pm

The value definitely set's once the type changes, which is after the value, however the warning is generated.

The code is below:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>React Re-Render Warning Example</title>
    <link rel="stylesheet" href="../shared/css/base.css" />
  </head>
  <body>
    <h1>React Re-Render Warning Example</h1>
    <div id="container">
      Loading...
    </div>
    <script src="../../build/react.js"></script>
    <script src="../../build/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
    <script type="text/babel">
      var InnerApp = React.createClass({
        getInitialState: function() {
          return {
            type: '',
            val: ''
          }
        },

        componentWillMount: function() {
          this.setState({
            type: this.props.type,
            val: this.props.val
          });
        },

        componentWillReceiveProps: function(nextProps) {
          this.setState({
            type: nextProps.type,
            val: nextProps.val
          });
        },

        onChange: function(event) {
          this.setState({
            val: event.target.value
          })
        },

        render: function() {
          var renderInput;
          if (this.state.type === 'text') {
            renderInput = (<input type='text' value={this.state.val} onChange={this.onChange} />);
          } else {
            renderInput = (<input type='number' value={this.state.val} onChange={this.onChange} />);
          }

          return (
            <div>
              {renderInput}
            </div>
          );
        }
      });

      var App = React.createClass({
        getInitialState: function() {
          return {
            type: 'text',
            val: ''
          }
        },

        changeType: function(itype) {
          this.setState({
            type: itype,
            val: itype === 'text' ? 'randomstring' : 32
          });
        },

        render: function() {
          return (
            <div>
              <button onClick={this.changeType.bind(null, 'text')}>Text</button>
              <button onClick={this.changeType.bind(null, 'number')}>Number</button>
              <InnerApp val={this.state.val} type={this.state.type} />
            </div>
          );
        }
      });

      ReactDOM.render(<App />, document.getElementById('container'));
    </script>
  </body>
</html>
@tisghans tisghans changed the title React re-render changed value field first before re-rendering the node React re-render changes <input> value field first before type and re-rendering the node Apr 30, 2016
@jimfb
Copy link
Contributor

jimfb commented Apr 30, 2016

Duplicate of #6441

@jimfb jimfb closed this as completed Apr 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants