<bdo id='xAxoY'></bdo><ul id='xAxoY'></ul>

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

      2. <tfoot id='xAxoY'></tfoot>

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

        使用地图时反应'无法读取未定义的属性'

        时间:2023-09-06
        <i id='BXW7W'><tr id='BXW7W'><dt id='BXW7W'><q id='BXW7W'><span id='BXW7W'><b id='BXW7W'><form id='BXW7W'><ins id='BXW7W'></ins><ul id='BXW7W'></ul><sub id='BXW7W'></sub></form><legend id='BXW7W'></legend><bdo id='BXW7W'><pre id='BXW7W'><center id='BXW7W'></center></pre></bdo></b><th id='BXW7W'></th></span></q></dt></tr></i><div id='BXW7W'><tfoot id='BXW7W'></tfoot><dl id='BXW7W'><fieldset id='BXW7W'></fieldset></dl></div>
      3. <tfoot id='BXW7W'></tfoot>

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

          • <legend id='BXW7W'><style id='BXW7W'><dir id='BXW7W'><q id='BXW7W'></q></dir></style></legend>

                <tbody id='BXW7W'></tbody>
                • <bdo id='BXW7W'></bdo><ul id='BXW7W'></ul>
                  本文介绍了使用地图时反应'无法读取未定义的属性'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从 teamtreehouse.com 制作一个非常基本的 React 应用程序,并且我经常遇到

                  I'm making a very basic React app from teamtreehouse.com, and I'm constantly encountering

                  TypeError:无法读取未定义的属性'onPlayerScoreChange'"

                  "TypeError: Cannot read property 'onPlayerScoreChange' of undefined"

                  即使我正确绑定了我的函数(我认为)

                  even though I'm binding my functions correctly (I think)

                  'onPlayerScoreChange'Grandparent 组件中的一个方法,当用户点击+"或-"按钮来更改玩家的分数时执行.

                  'onPlayerScoreChange' Is a method in the Grandparent component which executes when a user hits a '+' or '-' button to change a player's score.

                  如果有人能解释哪里出了问题,那将非常有帮助,因为我认为我在曾祖父母的构造函数中设置了 this.onPlayerScoreChange = this.onPlayerScoreChange.bind(this).

                  It would be really helpful if someone could explain what is wrong, because I think I am setting this.onPlayerScoreChange = this.onPlayerScoreChange.bind(this) in the great grandparent's constructor.

                  父组件:

                  class App extends React.Component {
                  constructor(props) {
                      super(props);
                      this.onPlayerScoreChange = this.onPlayerScoreChange.bind(this)
                      this.state = {
                          initialPlayers: props.initialPlayers,
                      };
                  }
                  
                  onPlayerScoreChange(delta, index) {
                      this.setState((prevState, props) => {
                          return {initialPlayers: this.prevState.initialPlayers[index].score += delta}
                      })
                  }
                  
                  render() {
                      return(
                          <div className = "scoreboard">
                              <Header title = {this.props.title}/>
                              <div className = "players">
                                  {this.state.initialPlayers.map(function(player, index) {
                                      return(
                                          <Player 
                                          name = {player.name} 
                                          score = {player.score} 
                                          key = {player.id} 
                                          index = {index}
                                          onScoreChange = {this.onPlayerScoreChange}
                                          />
                                      )
                                  })}
                              </div>
                          </div>
                      )
                  }}
                  

                  (组件具有标题的默认道具)

                  (Component has default props for title)

                  子组件:

                  class Player extends React.Component {
                  render() {
                      return(
                          <div className = "player">
                              <div className = "player-name">
                                  {this.props.name}
                              </div>
                              <div className = "player-score">
                                  <Counter score = {this.props.score} onChange = {this.props.onScoreChange} index = {this.props.index}/>
                              </div>
                          </div>
                  )
                  }}
                  

                  孙子组件:

                  class Counter extends React.Component {
                  constructor(props) {
                      super(props)
                      this.handleDecrement = this.handleDecrement.bind(this)
                      this.handleIncrement = this.handleIncrement.bind(this)
                  }
                  
                  handleDecrement() {
                      this.props.onChange(-1, this.props.index)
                  }
                  
                  handleIncrement() {
                      this.props.onChange(1, this.props.index)
                  }
                  
                  render() {
                      return(
                          <div className = "counter">
                              <button className = "counter-action decrement" onClick = {this.handleDecrement}> - </button>
                              <div className = "counter-score"> {this.props.score} </div>
                              <button className = "counter-action increment" onClick = {this.handleIncrement}> + </button>
                          </div>
                  )}}
                  

                  谢谢!

                  推荐答案

                  你使用的地图函数没有绑定 onScoreChange = {this.onPlayerScoreChange},

                  You have not done binding for the map function where you are using onScoreChange = {this.onPlayerScoreChange},

                  您可以使用绑定或箭头函数进行绑定

                  you can either use bind or arrow functions for binding

                  附:需要绑定,因为 map 函数的上下文与 React 组件上下文不同,因此此函数内的 this 不会引用 React 组件 this,因此您无法访问 React Component 类的属性.

                  P.S. Binding is needed because the context of the map function is different from the React Component context and hence this inside this function won't be Referring to the React Components this and thus you can't access that properties of the React Component class.

                  带箭头功能:

                   {this.state.initialPlayers.map((player, index)=> {
                                  return(
                                      <Player 
                                      name = {player.name} 
                                      score = {player.score} 
                                      key = {player.id} 
                                      index = {index}
                                      onScoreChange = {this.onPlayerScoreChange}
                                      />
                                  )
                              })}
                  

                  带绑定

                     {this.state.initialPlayers.map(function(player, index) {
                                  return(
                                      <Player 
                                      name = {player.name} 
                                      score = {player.score} 
                                      key = {player.id} 
                                      index = {index}
                                      onScoreChange = {this.onPlayerScoreChange}
                                      />
                                  )
                              }.bind(this))}
                  

                  这篇关于使用地图时反应'无法读取未定义的属性'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我什么时候应该使用内联和外部 Javascript? 下一篇:如何从 JavaScript 数字中删除小数部分?

                  相关文章

                  <legend id='TtKLy'><style id='TtKLy'><dir id='TtKLy'><q id='TtKLy'></q></dir></style></legend>

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

                    <bdo id='TtKLy'></bdo><ul id='TtKLy'></ul>

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