• <i id='SHcie'><tr id='SHcie'><dt id='SHcie'><q id='SHcie'><span id='SHcie'><b id='SHcie'><form id='SHcie'><ins id='SHcie'></ins><ul id='SHcie'></ul><sub id='SHcie'></sub></form><legend id='SHcie'></legend><bdo id='SHcie'><pre id='SHcie'><center id='SHcie'></center></pre></bdo></b><th id='SHcie'></th></span></q></dt></tr></i><div id='SHcie'><tfoot id='SHcie'></tfoot><dl id='SHcie'><fieldset id='SHcie'></fieldset></dl></div>

      • <bdo id='SHcie'></bdo><ul id='SHcie'></ul>

        <small id='SHcie'></small><noframes id='SHcie'>

        <tfoot id='SHcie'></tfoot>
        <legend id='SHcie'><style id='SHcie'><dir id='SHcie'><q id='SHcie'></q></dir></style></legend>

        (React.js)为什么react找不到对象?

        (React.js)Why react cannot find the object?((React.js)为什么react找不到对象?)

            <bdo id='khV2V'></bdo><ul id='khV2V'></ul>
              • <i id='khV2V'><tr id='khV2V'><dt id='khV2V'><q id='khV2V'><span id='khV2V'><b id='khV2V'><form id='khV2V'><ins id='khV2V'></ins><ul id='khV2V'></ul><sub id='khV2V'></sub></form><legend id='khV2V'></legend><bdo id='khV2V'><pre id='khV2V'><center id='khV2V'></center></pre></bdo></b><th id='khV2V'></th></span></q></dt></tr></i><div id='khV2V'><tfoot id='khV2V'></tfoot><dl id='khV2V'><fieldset id='khV2V'></fieldset></dl></div>
              • <small id='khV2V'></small><noframes id='khV2V'>

                  <tbody id='khV2V'></tbody>
                  <tfoot id='khV2V'></tfoot>
                  <legend id='khV2V'><style id='khV2V'><dir id='khV2V'><q id='khV2V'></q></dir></style></legend>
                  本文介绍了(React.js)为什么react找不到对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是一个新手,正在学习使用 React.js 构建一个 rpg 游戏,我发现这部分非常混乱.

                  I am a newbie learning to build a rpg game with React.js, and I found this part very confusing.

                  我正在尝试使用 onClick 更新 life,当 life<damage 时,对象 播放器 被丢弃.所以html看起来像这样:

                  I am trying to update life with onClick and when life<damage, the object player is dropped. So the html looks like this:

                  当前播放器是这样生成的:

                  The current player is generated like this:

                  <h4>You are: {this.props.targets[this.props.playerindex].name}</h4>
                  

                  我使用这些代码来处理攻击和更新索引:

                  I use these code to handle attack and update the index:

                  handleAttack(index1,id2){
                      let players = this.state.players.slice();
                      let index2 = players.findIndex(x => x.id === id2);
                      let damage = players[index1].damage;
                      let life = players[index2].life;
                      console.log("Before(length):"+this.state.players.length);
                      if (life>damage){
                          players[index2].life = life-damage;}
                      else {players=players.filter(player => player.id !== id2);}
                      this.setState({players},()=>{console.log("After(length):"+this.state.players.length);
                                                   this.handleUpdateIndex();});
                  }
                  
                  handleUpdateIndex(){
                      console.log("Before:"+this.state.playerindex);
                      let index=this.state.playerindex;
                      let length=this.state.players.length;
                      console.log("BeforeUpdate(length)"+this.state.players.length);
                      if(index<length-1)
                      {this.setState({playerindex:index+1},() => {console.log("After:"+this.state.playerindex);});}
                      else{this.setState({playerindex:0},() => {console.log("After:"+this.state.playerindex);});}
                      this.forceUpdate();
                  }
                  

                  但有时索引会增加而它不应该增加,并导致:

                  But sometimes the index will increment while it should not, and causes this:

                  我觉得可能是setState的异步行为,但是不知道怎么解决这个问题.

                  I think it might be the asynchronous behavior of setState, but I don't know how should I solve this problem.

                  如果您有实现预期行为的解决方案或其他方法,请提供帮助!

                  If you have a solution or another way to achieve the expected behavior, please help!

                  代码在这里:

                  App.js:https://ghostbin.com/paste/e9wjg

                  Attack.js:https://ghostbin.com/paste/3zbwk

                  推荐答案

                  我知道我错了:

                  当我删除一个对象时,我应该将索引向后移动:

                  when I am dropping a object, I should move the index back:

                     if (life>damage){
                          players[index2].life = life-damage;}
                      else {players=players.filter(player => player.id !== id2);
                            this.setState({playerindex:this.state.playerindex-1});}
                  

                  这个问题已经解决了,但是如果你愿意,请给我这个项目的建议!

                  This problem is solved, however please give me advice on this project if you like!

                  这篇关于(React.js)为什么react找不到对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                    1. <tfoot id='yql0u'></tfoot>
                        • <bdo id='yql0u'></bdo><ul id='yql0u'></ul>

                          <i id='yql0u'><tr id='yql0u'><dt id='yql0u'><q id='yql0u'><span id='yql0u'><b id='yql0u'><form id='yql0u'><ins id='yql0u'></ins><ul id='yql0u'></ul><sub id='yql0u'></sub></form><legend id='yql0u'></legend><bdo id='yql0u'><pre id='yql0u'><center id='yql0u'></center></pre></bdo></b><th id='yql0u'></th></span></q></dt></tr></i><div id='yql0u'><tfoot id='yql0u'></tfoot><dl id='yql0u'><fieldset id='yql0u'></fieldset></dl></div>

                              <tbody id='yql0u'></tbody>
                          1. <legend id='yql0u'><style id='yql0u'><dir id='yql0u'><q id='yql0u'></q></dir></style></legend>

                            <small id='yql0u'></small><noframes id='yql0u'>