无法在现有状态转换期间更新

Cannot update during an existing state transition

我的流星项目在我加载时总是让我的浏览器崩溃。只有在 App.jsx 文件中注释掉 this.setState({input_36:currentApp.input_36}); 才能避免浏览器崩溃。有人可以告诉我如何修复我的代码,以便项目可以加载而不会崩溃,并且如果您单击 <ul> 中的任何超链接,它会重新呈现表单吗?并通过确保存在 href= 属性来确保链接符合 WCAG 标准并优化搜索引擎?

这是我的项目...我在终端命令行中做的

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 以启动项目。然后我转到我的网络浏览器。将出现一个文本字段,以便您可以键入内容并按几次 Enter,您创建的每个表单的列表将自动出现。

接下来,我通过取消注释粗体行来修改 App.jsx。该项目将重新编译。然后我转到我的网络浏览器,它由于无限循环和错误消息

而冻结

Cannot update during an existing state transition (such as within "render"). Render methods should be a pure function of props and state.

如何解决这种情况,以便加载项目并单击链接重新呈现表单?

约翰说: 改变 onClick={_this.clickLoadForm(applicationform._id)}onClick={_this.clickLoadForm.bind(_this,applicationform._id)}

如果这不起作用,则可能是下面的一些变体


尝试将 _this.clickLoadForm(_this.props.router.params.appid) 更改为 _this.clickLoadForm

<a  onClick={_this.clickLoadForm}
    data-value={_this.props.router.params.appid}
    href={Meteor.absoluteUrl()+'application/' +applicationform._id}
    key={"a"+i}>
    Version {applicationform._id}
</a>


clickLoadForm(e) {
    var appId = e.target.dataset.value;
    var currentApp = this.loadForm(appId);
    this.setState({input_36:currentApp.input_36});
}

您似乎已经在执行 clickLoadForm 函数,因此触发了 this.setState

可能会更改为 "onClick={_this.clickLoadForm.bind(_this,_this.props.router.params.appid)}"