js中的react庫基本運作原理的理解

1、創建一個react項目

npx create-react-app my-app

2、運行一個react項目

npm start

3、簡單的js代碼示意

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 {
	constructor(props){
		super(props);
		this.state = {
			squares : Array(9).fill(null), 
			xIsNext: true,
		};
	}

	handleClick(i){
		const squares = this.state.squares.slice();
		if (calculateWinner(squares) || squares[i]) {
      			return;
    		}
		squares[i] = this.state.xIsNext ? 'X' : 'O';
		this.setState({
			squares: squares,
			xIsNext: !this.state.xIsNext,
		});
	}

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

  render() {
  	const winner = calculateWinner(this.state.squares);
  	let status;
  	if (winner){
  		status = 'winner :' + winner;
  	}else{
  		status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
  	}

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

對react框架的一些基本的理解
A、我認爲整個代碼,是從board模塊的render開始渲染的,將內容渲染到頁面上面。
B、整個代碼,觸發事件是主線,下面是我對觸發事件的一下理解。
1、 鼠標點擊空白處的按鈕標籤,這時的button已經被註冊好了onclick的觸發事件,是通過props傳輸過來了的
2、render會記得你在哪裏點擊這個事件,比如說是在1上面觸發了

 {this.renderSquare(1)}

這樣,就會調用renderSquare()方法,這個方法會先執行value = {this.state.squares[i]} 爲數組賦值的操作,這個數組用來記錄哪個位置已經被賦值了,其次,會進行onClick = {() => this.handleClick(i)} 這個操作,進行數組的複製const squares = this.state.squares.slice();判斷是否應該繼續遊戲 if (calculateWinner(squares) || squares[i]) ,爲提示下一步誰進行操作重新渲染squares[i] = this.state.xIsNext ? ‘X’ : ‘O’; 爲數組賦值squares: squares,下家的標誌取反xIsNext: !this.state.xIsNext,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章