GithubHelp home page GithubHelp logo

react-tic-tac-toe's Introduction

react-tic-tac-toe

When you were passing onSquareClick={handleClick}, you were passing the handleClick function down as a prop. You were not calling it! But now you are calling that function right away—notice the parentheses in handleClick(0)—and that’s why it runs too early. You don’t want to call handleClick until the user clicks!

問題出在:之前是傳一個 function ,現在是直接執行這個 function , 應該要等 click 後才執行。

  function handleClick(i) {
    const nextSquares = squares.slice();
    nextSquares[i] = "X";
    setSquares(nextSquares);
  }

<Square value={squares[0]} onSquareClick={handleClick(0)}/>

To fix this, you could create a function like handleFirstSquareClick that calls handleClick(0), a function like handleSecondSquareClick that calls handleClick(1), and so on. Instead of calling them, you would pass these functions down as props like onSquareClick={handleFirstSquareClick}. This would solve the infinite loop.

解決方法是要傳一個 fucntion 該 function 是有參數 0 的,參數 1的,等等…

<Square value={squares[0]} onSquareClick={() => {handleClick(0)}}/>

save history in Game()

ref : https://beta.reactjs.org/learn/tutorial-tic-tac-toe#lifting-state-up-again

history = [[squares] + nextSquares] = [...squares,nextSquares];

put history in Game()

currentSquares = history[history.length - 1];

pass currentSquares into Board()

handleChick 中執行 onPlay()

onPlay() in Game()

onPlay() save history & currentSquares & xIsNext

react-tic-tac-toe's People

Contributors

wllcrre avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.