javascript - onClick 绑定在 React 中究竟是如何工作的?
问题描述
我是第一次学习 React JS,因为这是我的团队在新项目中可能采用的方法。我试图了解这个 onClick 绑定行为以及它到底在做什么。
我查看了一些文章,这些文章试图解释将组件/对象的特定实例绑定到它们各自的功能,它们有点道理。但是使用 'this.props.onClick(i)' 对我来说没有多大意义。
那么该代码是否将 squares[i] 作为道具传递,并且在 Square 组件中以某种方式 onClick 这会更新按钮的值?运行程序时,它似乎在做,但我似乎无法理解这个逻辑。特别是因为我来自 C# 和 Java 的后端背景。任何帮助将不胜感激!
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
class Board extends React.Component {
renderSquare(i) {
return (
<Square
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}
render() {
return (
<div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
更新:整个应用程序。为了提供更多上下文...
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
class Board extends React.Component {
renderSquare(i) {
return (
<Square
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}
render() {
return (
<div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
history: [{
squares: Array(9).fill(null)
}],
xIsNext: true,
};
}
handleClick(i) {
const history = this.state.history;
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
history: history.concat([{
squares: squares
}]),
xIsNext: !this.state.xIsNext,
});
}
render() {
const history = this.state.history;
const current = history[history.length - 1];
const winner = calculateWinner(current.squares);
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{/* TODO */}</ol>
</div>
</div>
);
}
}
// ========================================
ReactDOM.render(
<Game />,
document.getElementById('root')
);
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] == squares[b] && squares[a] == squares[c]) {
return squares[a];
}
}
return null;
}
解决方案
反应的整个想法是提升你的状态。在这段代码中,
游戏是一切的父级,它将定义当我单击子 Square 时会发生什么。不是 Square 自己处理它,Game 继续处理 onClick 并作为这样的道具传递给棋盘。
<Board squares={current.squares} onClick={(i) => this.handleClick(i)} />
现在 Board 反过来将它在从 Game 调用时收到的道具 onClick 传递给它的子 Square,就像这样作为道具。
<Square value={this.props.squares[i]} onClick={() => this.props.onClick(i)} />
所以 Game 的 handleClick 被分配给 Board 的 onClick 属性,它被分配给 Square 的 onClick 属性。因此 Square 告诉 Board 我被点击了,但我不知道如何处理它。所以我会把责任转移给你,当你打电话给我(Square)时,你可以把你传给我的任何东西都称为道具。Board 做同样的事情并将责任传递给 Game,它实际上知道 onClick 我应该调用我内部的 handleClick 事件。
handleClick(i) {
const history = this.state.history;
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
history: history.concat([{
squares: squares
}]),
xIsNext: !this.state.xIsNext,
});
}
handleClick in Game passed to Board as onClick prop -> onClick prop of Board passed to Square as onClick prop
因此,点击 Square 就变成了反向操作
Square says my onClick prop is actually passed by Board -> Board says my onClick prop is actually passed by Game -> Game says I know how to handleClick
推荐阅读
- sql - Oracle Sql 转置表
- android - 有什么方法可以将firebase中的身份验证限制为仅一个用户
- python - Django 自定义登录装饰器检查会话是否存在
- java - 本地容器上的 java springboot 应用程序无法访问 aws
- oracle - ALL_LOG_GROUPS 不包含有关当前用户可访问的表的信息
- r - 向现有条形图添加多个 y 轴和数据
- flutter - 将 onPress() FlatButton 转换为功能 dart 时出错
- javascript - 在 React Native 中使用 onPress 打开屏幕的问题
- node.js - Express 中间件 - 调用周期
- algorithm - 如果一个优化问题在 L1-metric 下是 NP-hard,那么对于 L2-metric 下的问题我们能说什么呢?