在文本区域上反应 setState 被忽略
React setState on textarea ignored
我正在开发一个简单的流星 + React CRUD 应用程序。在下面的代码中,我的 this.setState()
似乎对 <textarea>
没有任何影响。因此,当我单击 link 并重新呈现表单时,函数 clickLoadForm(appId)
会正确更新 <input>
元素的状态,但不会更新 <textarea>
,即使 console.log 清楚地表明所有字段都有内容。我做错了什么?
App = React.createClass({
mixins: [ReactMeteorData],
getMeteorData() {
return {
applications: Applications.find({}, {sort: {createdAt: -1}}).fetch(),
currentApplication: Applications.findOne({_id:this.props.router.params.appid}, {sort: {createdAt: -1}}),
}
},
getInitialState: function() {
return this.loadForm(this.props.router.params.appid);
},
loadForm(appId) {
var currentApp = Applications.findOne({_id:appId});
if(!currentApp) currentApp = {};
return currentApp;
},
clickLoadForm(appId)
{
var currentApp = this.loadForm(appId);
var state = new Object();
var refs = this.refs;
Object.keys(refs).map(function(prop,index){
state[prop] = typeof currentApp[prop] == 'undefined' ? "" : currentApp[prop];
});
console.log(state);
this.setState(state);
},
renderListApplications() {
var _this = this;
return this.data.applications.map(function(applicationform,i) {
return <li key={"li"+i}><a onClick={_this.clickLoadForm.bind(_this,applicationform._id)} href={Meteor.absoluteUrl()+'application/' +applicationform._id} key={"a"+i}>Version {applicationform._id}</a></li>;
});
},
handleSubmit(event) {
event.preventDefault();
var refs = this.refs;
var formVals = new Object();
Object.keys(refs).map(function(prop, index){
if(refs[prop].nodeName.match(/(INPUT|SELECT|TEXTAREA)/).length > 0)
formVals[prop] = refs[prop].value;
});
Meteor.call("saveApplication", formVals);
},
handleChange: function(e) {
if(!e.target.id) return;
if(typeof e.target.id == 'undefined') return;
var state = new Object();
state[e.target.id] = e.target.value;
this.setState(state);
},
render() {
return (
<div className="container">
<ul>
{this.renderListApplications()}
</ul>
<div>{JSON.stringify(this.data.currentApplication)}</div>
<form className="new-task" onSubmit={this.handleSubmit} >
<input ref="input_36" id="input_36" type="text" value={this.state.input_36} onChange={this.handleChange} />
<input ref="input_37" id="input_37" type="text" value={this.state.input_37} onChange={this.handleChange} />
<textarea ref="input_38" id="input_38" onChange={this.handleChange}>{this.state.input_38}</textarea>
<textarea ref="input_39" id="input_39" onChange={this.handleChange}>{this.state.input_39}</textarea>
<button type="submit">Submit</button>
</form>
</div>
);
}
});
其实我想通了。我变了
<textarea ref="input_38" id="input_38" onChange={this.handleChange}>{this.state.input_38}</textarea>
<textarea ref="input_39" id="input_39" onChange={this.handleChange}>{this.state.input_39}</textarea>
到
<textarea ref="input_38" id="input_38" onChange={this.handleChange} value={this.state.input_38} />
<textarea ref="input_39" id="input_39" onChange={this.handleChange} value={this.state.input_39} />
我正在开发一个简单的流星 + React CRUD 应用程序。在下面的代码中,我的 this.setState()
似乎对 <textarea>
没有任何影响。因此,当我单击 link 并重新呈现表单时,函数 clickLoadForm(appId)
会正确更新 <input>
元素的状态,但不会更新 <textarea>
,即使 console.log 清楚地表明所有字段都有内容。我做错了什么?
App = React.createClass({
mixins: [ReactMeteorData],
getMeteorData() {
return {
applications: Applications.find({}, {sort: {createdAt: -1}}).fetch(),
currentApplication: Applications.findOne({_id:this.props.router.params.appid}, {sort: {createdAt: -1}}),
}
},
getInitialState: function() {
return this.loadForm(this.props.router.params.appid);
},
loadForm(appId) {
var currentApp = Applications.findOne({_id:appId});
if(!currentApp) currentApp = {};
return currentApp;
},
clickLoadForm(appId)
{
var currentApp = this.loadForm(appId);
var state = new Object();
var refs = this.refs;
Object.keys(refs).map(function(prop,index){
state[prop] = typeof currentApp[prop] == 'undefined' ? "" : currentApp[prop];
});
console.log(state);
this.setState(state);
},
renderListApplications() {
var _this = this;
return this.data.applications.map(function(applicationform,i) {
return <li key={"li"+i}><a onClick={_this.clickLoadForm.bind(_this,applicationform._id)} href={Meteor.absoluteUrl()+'application/' +applicationform._id} key={"a"+i}>Version {applicationform._id}</a></li>;
});
},
handleSubmit(event) {
event.preventDefault();
var refs = this.refs;
var formVals = new Object();
Object.keys(refs).map(function(prop, index){
if(refs[prop].nodeName.match(/(INPUT|SELECT|TEXTAREA)/).length > 0)
formVals[prop] = refs[prop].value;
});
Meteor.call("saveApplication", formVals);
},
handleChange: function(e) {
if(!e.target.id) return;
if(typeof e.target.id == 'undefined') return;
var state = new Object();
state[e.target.id] = e.target.value;
this.setState(state);
},
render() {
return (
<div className="container">
<ul>
{this.renderListApplications()}
</ul>
<div>{JSON.stringify(this.data.currentApplication)}</div>
<form className="new-task" onSubmit={this.handleSubmit} >
<input ref="input_36" id="input_36" type="text" value={this.state.input_36} onChange={this.handleChange} />
<input ref="input_37" id="input_37" type="text" value={this.state.input_37} onChange={this.handleChange} />
<textarea ref="input_38" id="input_38" onChange={this.handleChange}>{this.state.input_38}</textarea>
<textarea ref="input_39" id="input_39" onChange={this.handleChange}>{this.state.input_39}</textarea>
<button type="submit">Submit</button>
</form>
</div>
);
}
});
其实我想通了。我变了
<textarea ref="input_38" id="input_38" onChange={this.handleChange}>{this.state.input_38}</textarea>
<textarea ref="input_39" id="input_39" onChange={this.handleChange}>{this.state.input_39}</textarea>
到
<textarea ref="input_38" id="input_38" onChange={this.handleChange} value={this.state.input_38} />
<textarea ref="input_39" id="input_39" onChange={this.handleChange} value={this.state.input_39} />