【问题标题】:React JS: Understanding StateReact JS:理解状态
【发布时间】:2020-02-10 04:21:27
【问题描述】:

我是 react JS 的初学者,正在尝试完成他们在其网站上提供的井字游戏教程,但我很难理解如何通过代码更改状态。

这是游戏使用的 2 个组件的 sn-p:

class Square extends React.Component {

   render() 
  {
    return (
      <button className="square"
        onClick={() => this.props.onClick()}
        >
      {this.props.value} 
      </button>
    );
  }
}

class Board extends React.Component {

    constructor(props)
  {
    super(props);
    this.state =
     {
      squares: Array(9).fill(null),
    };
  }

    handleClick(i) 
  {
    const squares = this.state.squares.slice();
    squares[i] = 'X';
    this.setState({squares: squares});
  }

  renderSquare(i) 
  {
   return (
      <Square
        value={this.state.squares[i]}
        onClick={() => this.handleClick(i)}
      />
    );
  }

  render() {
    const status = 'Next player: X';

    return (
      <div>
        <div className="status">{status}</div>
        <div className="board-row">
          {this.renderSquare(0)}
//this.renderSquare is called 7 more times to create the board 
- left out to make code here look slightly more concise

        </div>
    );
  }
}

以下是我不明白的地方: 首先调用 renderSquare,传入 0 作为 i 的参数。然后调用 square 组件,传入两个属性 - value 和 onClick。我只是不明白正方形组件中发生了什么。 onClick 被分配了在 renderSquare 的 return 语句中声明的匿名函数,但是如何/何时调用 onClick?我不太确定 onClick() 在这种情况下是如何工作的。

我在这里阅读过关于状态的类似问题,但我不理解给出的解释。任何帮助,将不胜感激。

【问题讨论】:

    标签: reactjs react-state-management react-state


    【解决方案1】:

    &lt;Square&gt; 呈现一些 HTML,如您在 Square 类中所见。该 HTML 包含一个带有 prop 的 div 元素:onClick={()=&gt;this.props.onClick()} React 会将其转换为标准的 Javascript onclick 事件。当用户单击 div 时调用的单击处理程序是作为 onClick 属性传递给 Square 组件的任何函数:在本例中为 this.handleClick(i)(其中 i 的值已传递到renderSquare 函数)。到目前为止和我在一起?

    this.handleClick(i)被调用时,它使用setStatethis.state.squares[i]的值设置为'X'。该值在每个 Square (value={this.state.squares[i]}) 内被拾取,并由 {this.props.value} 在 Square 内渲染。因此,当用户点击一个正方形时,正方形内会呈现一个 X。

    我希望这可以澄清事情!

    【讨论】:

      【解决方案2】:

      但是如何/何时调用 onClick?

      只要在浏览器中单击 Square,就会调用onClick

      更具体地说,当调用 Square 组件中按钮中的 onClick 时,将调用 onClick

      class Square extends React.Component {
      
         render() 
        {
          return (
            <button className="square"
              // here!
              onClick={() => this.props.onClick()}
              >
            {this.props.value} 
            </button>
          );
        }
      }
      

      这个button.onClick 是一个React 提供的函数,它与按钮的onclick 方法挂钩,就像普通的javascript 一样。

      【讨论】:

        【解决方案3】:

        所以要充分解释这里发生的事情,首先要了解为什么尽可能将组件逻辑保留在父组件中很重要。这样做是为了让 square 组件在未来更具可重用性,因为 square 组件的工作只是渲染简单的 html 而无需担心其他任何事情。

        所以当你去渲染正方形时,我们实际上是在传递父组件函数作为道具:

        renderSquare(i) 
        {
           return (
              <Square
                value={this.state.squares[i]}
                onClick={() => this.handleClick(i)}
              />
            );
          }
        

        现在想象一下,如果 square 是一个函数,它所接受的参数是一个函数,它基本上看起来像这样

        function Square(onClick) {
          //so now you have access to the parents "handleClick" method
        }
        

        这是上面的一个提炼示例,但实际上它会在 props 对象中,因此您可以在示例中使用 this.props.onClick 访问它。所以当正方形被点击时,它会像this.props.onClick()一样运行函数。请记住,这是调用传递给 square 的父组件 handleClick 方法。 i 也缓存在函数调用中,因此如果您单击第 6 个方块,其索引将为 5。因此,当它设置状态时,它将第 6 个方块(数组中的 5 个索引)的状态设置为 ' x' 在父组件中。

        现在,由于设置了状态,组件将根据生命周期方法在 React 中的工作方式重新渲染。所以它会循环遍历存储的每个状态,并将该值传递给它应该显示的方块。

        编辑:另外,如果您想知道反应中给您的事件,我建议您阅读这两个:

        https://reactjs.org/docs/events.html https://reactjs.org/docs/handling-events.html

        【讨论】:

        • 我被这个命令弄糊涂了:onClick={() => this.props.onClick()} (这是在方形组件中) 这怎么调用手柄点击按钮?没有任何东西被传递到该函数中,所以对我来说有点断开连接。
        • React 在 html 元素上有默认事件。所以 onClick 是一个反应事件,它正在执行一个匿名函数,该函数调用 props 中父级传递的 onClick。该 props.onClick 可以命名为任何名称,但他们选择将其命名为 onClick,这可能会增加混乱。我实际上会将 props.onClick 的名称更改为 onSquareClick。
        • 这开始有意义了。为什么在放置 this.props.onclick 之前必须有“onClick={() =>”?为什么不能只放“this.props.onclick”?
        • onClick 基本上是一个在遇到动作时触发的事件。所以普通的html实体可以注册一个onClick,即使它会触发一个回调函数。因此,当您将 onClick 函数放在 html 项目上时,它有一个回调,您可以将函数传递给该回调。对于组件,设置 onClick 基本上就像传递一个名为“onClick”的道具,因此在这种情况下它的工作方式不同。但同样对于 html 元素,您正在访问一个需要回调的本机反应事件。
        猜你喜欢
        • 2017-11-26
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        • 2020-07-13
        • 2021-07-30
        • 2017-09-06
        • 2018-09-18
        • 1970-01-01
        相关资源
        最近更新 更多