gpt4 book ai didi

javascript - React 中的回调参数如何从子组件传递到父组件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:34 24 4
gpt4 key购买 nike

我一直在 https://reactjs.org/tutorial/tutorial.html 学习 React 教程我过得很好。我有一件事我一直无法理解。这是完整的代码(代码笔:https://codepen.io/gaearon/pen/EmmOqJ?editors=0010):

function Square(props) {  
return (
<button className="square"
onClick={props.onClick}>

{props.value}
</button>
);
}

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;
}

class Board extends React.Component {
renderSquare(i) {
return (
<Square value={this.props.squares[i]} onClick={() => this.props.onClick(i)}/>
);
}

render() {
return (
<div>
<div className="status">{status}</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')
);

在教程中,他们将回调函数 handleClick(i) 从父 Game 组件传递给子 Board 组件,并从那里到子 Square 组件。我的问题是参数 i 是如何设置的?我的猜测是起点是在 Board 组件中调用 renderSquare(i) 时。从那里我不知道 i 它是如何到达 handleClick(i) 的。它是否存储在“从 Board 传递给Square的 onClick函数对象中”?






最佳答案






My guess is that the starting point is when renderSquare(i) is called in the Board component. From there I am lost as to how i it makes its way to handleClick(i)




你走在正确的轨道上。



Board.render()内, this.renderSquare(...)被调用号码 1~8 .



renderSquare有一个 onClick处理程序,this.props.onClick(i) .



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

this.props从父级传递,Game.render() .
所以this.props.onClick来自<Board onClick={...} />

class Game extends React.Component {
handleClick(i) {
...
}

render() {
...
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
...
</div>
);
}
}

所以 this.props.onClick匹配<Board onClick={...} />
它被实现为 (i) => this.handleClick(i) , 其中this.handleClick(i)Game.handleClick(i) .

关于javascript - React 中的回调参数如何从子组件传递到父组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53454780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com