当我加载它时,我的meteor项目不断崩溃我的浏览器 . 如果我在App.jsx文件中注释 this.setState({input_36:currentApp.input_36});
,我只能避免浏览器崩溃 . 有人可以告诉我如何修复我的代码,以便项目可以加载而不会崩溃,如果你点击 <ul>
中的任何超链接,它将重新呈现表单?并确保链接符合WCAG并通过确保 href=
属性存在优化搜索引擎?
这是我的项目...在终端命令行中我做的
meteor create crudapp
cd crudapp
rm crudapp.js
meteor remove autopublish
meteor add react
meteor add iron:router
然后我的项目中有以下文件
crudapp.html
<head>
<title>Application Form</title>
</head>
<body>
<div id="render-target"></div>
</body>
crudapp.jsx
Applications = new Mongo.Collection("applications");
if(Meteor.isServer)
{
Meteor.publish("applications", function(){
return Applications.find();
});
}
var configAppRoute = {waitOn:function(){return [Meteor.subscribe('applications')]},action:applicationController};
Router.route('/application',configAppRoute);
Router.route('/application/:appid',configAppRoute);
function applicationController()
{
var router = this;
Meteor.startup(function () {
ReactDOM.render(<App router={router} />, document.getElementById("render-target"));
});
}
Meteor.methods({
saveApplication(formVals) {
formVals['createdAt'] = new Date();
Applications.insert(formVals);
}
});
App.jsx
App = React.createClass({
mixins: [ReactMeteorData],
getMeteorData() {
return {
applications: Applications.find({}, {sort: {createdAt: -1}}).fetch(),
}
},
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);
//this.setState({input_36:currentApp.input_36});
},
renderListApplications() {
var _this = this;
return this.data.applications.map(function(applicationform,i) {
return <li key={"li"+i}><a onClick={_this.clickLoadForm(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) {
this.setState({ input_36: e.target.value });
},
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" type="text" tabIndex="1" value={this.state.input_36} onChange={this.handleChange} />
<button type="submit">Submit</button>
</form>
</div>
);
}
});
然后我转到命令行并输入 meteor
以启动项目 . 然后我去我的网络浏览器 . 出现一个文本字段,您可以键入内容并按几次输入,并自动显示您创建的每个表单的列表 .
接下来,我通过取消注释粗体线来修改App.jsx . 该项目将重新编译 . 然后我进入我的网络浏览器,因为带有错误消息的无限循环而冻结
在现有状态转换期间无法更新(例如在“渲染”中) . 渲染方法应该是道具和状态的纯函数 .
如何解决这种情况,以便它将加载项目并单击链接将重新呈现表单?
2 回答
也许更改为“onClick = {_ this.clickLoadForm.bind(_this,_this.props.router.params.appid)}”
约翰说:将
onClick={_this.clickLoadForm(applicationform._id)}
改为onClick={_this.clickLoadForm.bind(_this,applicationform._id)}
如果这不起作用,那么可能是下面的一些变化
尝试将
_this.clickLoadForm(_this.props.router.params.appid)
更改为_this.clickLoadForm
看起来你已经在执行
clickLoadForm
函数从而触发this.setState