admin管理员组文章数量:1334342
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 ofonChange
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 aDate
. When the user types9:15
this is a sequence of characters -9
,:
, etc. If9
by itself is interpreted as9:00am
we do not want the input box to change instantly to9: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
1 Answer
Reset to default 6You 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>
);
}
本文标签: javascriptReact input text default value and onblur function make input text read onlyStack Overflow
版权声明:本文标题:javascript - React input text default value and onblur function make input text read only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742272020a2444483.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论