最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React input text default value and onblur function make input text read only - Stack Overflow

programmeradmin5浏览0评论

guys, I am working on react js

I have created an input text and on blur of that input button I am calling a method and updating the state variable

but seem like if I use on blur instead of onChange then input text bee read only field

<!DOCTYPE html>
<html>

  <head>
    <script data-require="react@*" data-semver="0.13.3" src=".13.3/react.js"></script>
    <script data-require="react-jsx@*" data-semver="0.13.1" src=".13.1/JSXTransformer.js"></script>

    <link rel="stylesheet" href="style.css" />
    <script type="text/jsx">

      var App = React.createClass({
          getInitialState: function () {
              return {value: 'Hello!'};
          },

          changeTo: function (e) {
              this.setState({value: e.target.value});
          },

          render: function () {
              return (
                  <div>{this.state.value}
                      <input type="text" defaultValue={this.state.value || ''} 
                         value={this.state.value} onBlur={(e) => this.changeTo(e)} />
                  </div>
              );
          }
      });

      React.render(
          <App />,
          document.getElementById('app')
      );
    </script>
  </head>

  <body>
    <div id="app"></div>
  </body>

</html>

guys, I am working on react js

I have created an input text and on blur of that input button I am calling a method and updating the state variable

but seem like if I use on blur instead of onChange then input text bee read only field

<!DOCTYPE html>
<html>

  <head>
    <script data-require="react@*" data-semver="0.13.3" src="http://cdnjs.cloudflare./ajax/libs/react/0.13.3/react.js"></script>
    <script data-require="react-jsx@*" data-semver="0.13.1" src="http://cdnjs.cloudflare./ajax/libs/react/0.13.1/JSXTransformer.js"></script>

    <link rel="stylesheet" href="style.css" />
    <script type="text/jsx">

      var App = React.createClass({
          getInitialState: function () {
              return {value: 'Hello!'};
          },

          changeTo: function (e) {
              this.setState({value: e.target.value});
          },

          render: function () {
              return (
                  <div>{this.state.value}
                      <input type="text" defaultValue={this.state.value || ''} 
                         value={this.state.value} onBlur={(e) => this.changeTo(e)} />
                  </div>
              );
          }
      });

      React.render(
          <App />,
          document.getElementById('app')
      );
    </script>
  </head>

  <body>
    <div id="app"></div>
  </body>

</html>
Share Improve this question asked Sep 27, 2016 at 12:19 Ratan PaulRatan Paul 4929 silver badges27 bronze badges 4
  • if you use only onBlur, then input bee uncontrolled. You should use onChange to reflect(for controlled input) the change. What actually the question is? – Fazal Rasel Commented Sep 27, 2016 at 12:50
  • 1 I don't want to use onchange instead can't i just use the onblur ? if so then what will be solution to use onblur – Ratan Paul Commented Sep 27, 2016 at 12:57
  • for reflecting change, use onChange... for any other special case for blur(like updating other things), use onBlur. – Fazal Rasel Commented Sep 27, 2016 at 12:59
  • 2 A typical reason to use onBlur/onfocusout instead of onChange is that the input box state is interpreted and has invalid (or valid but problematic) states. Imagine if the textbox represents time-of-day, so the internal state is a Date. When the user types 9:15 this is a sequence of characters - 9, :, etc. If 9 by itself is interpreted as 9:00am we do not want the input box to change instantly to 9:00am. Handling focusout/onblur is a well-known classic way to defer reinterpretation of a textbox, but React bizarrely prevents it. – Qwertie Commented Jun 14, 2018 at 23:04
Add a ment  | 

1 Answer 1

Reset to default 6

You need to add onChange listener otherwise it will not work.

Reason: When you specify value={this.state.value} it means you are binding input text value to the variable value and react takes over the control for updating value in input box. By adding onChange listener, you tell react to call a ponent function to update value which basically updates the state of the ponent forcing it to call render function and updating the value in input text.

      changeTo: function (e) {
          this.setState({value: e.target.value});
      },

      updateValue: function (e) {
          this.setState({value: e.target.value});
      },

      render: function () {
          return (
              <div>{this.state.value}
                  <input type="text" defaultValue={this.state.value || ''} value={this.state.value} 
onBlur={(e) => this.changeTo(e)} onChange={(e) => this.updateValue(e}/>
              </div>
          );
      }
发布评论

评论列表(0)

  1. 暂无评论