react tic tac toe Flashcards

(34 cards)

1
Q

In the tutorial starter code, what does the Square component return?

A

A single button element with className=’square’. It uses className (not class) for CSS styling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the role of index.js in the tutorial project?

A

It bridges components and the browser. It imports React, ReactDOM, styles, and the App component, then renders everything into the DOM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What error do you get if you return two adjacent JSX elements without a wrapper?

A

‘Adjacent JSX elements must be wrapped in an enclosing tag.’ Fix by wrapping in a Fragment (<>…</>) or a <div>.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the Board pass different values to each Square?

A

Via the value prop: <Square value={squares[0]} />, <Square value={squares[1]} />, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does Square read a prop passed from Board?

A

By destructuring in the function signature: function Square({ value }). Then use {value} in JSX to display it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between writing {value} and value in JSX?

A

Curly braces render the JavaScript variable. Without them, JSX renders the literal text ‘value’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you make a Square interactive (respond to clicks)?

A

Declare a handleClick function inside Square, then pass it to onClick: <button onClick={handleClick}>.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does useState work in the Square component?

A

const [value, setValue] = useState(null) — value holds current state (initially null), setValue updates it and triggers re-render.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When setValue(‘X’) is called, what happens?

A

React re-renders the Square component with value now equal to ‘X’, so X appears on screen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is state shared between different Square instances?

A

No. Each Square has its own independent state. Clicking one does not affect the others.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is game state moved from individual Squares into the Board?

A

The Board needs to know all 9 squares to check for a winner. Keeping state in each Square makes coordination difficult.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Board initialize its state for the 9 squares?

A

const [squares, setSquares] = useState(Array(9).fill(null)) — an array of 9 nulls, one per square.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Since state is private to Board, how does Square update the board?

A

Board passes a callback (onSquareClick) as a prop. Square calls it on click, triggering Board’s handleClick.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why does onSquareClick={() => handleClick(0)} use an arrow function?

A

To pass a function reference that calls handleClick(0) on click. Without the arrow, it would call immediately during render.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why does handleClick(0) without an arrow function cause an infinite loop?

A

It runs during render, calls setSquares, triggers re-render, runs handleClick(0) again — infinite loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does handleClick do to update the board?

A

Creates a copy with .slice(), sets the clicked index to X or O, then calls setSquares(nextSquares) to re-render.

17
Q

Why does handleClick use squares.slice() instead of modifying the array directly?

A

To preserve immutability. A new copy enables time-travel, cheap change detection, and keeps previous states intact.

18
Q

How does immutability help React performance?

A

React can check if data changed via reference comparison (===) instead of deep-comparing the entire object tree.

19
Q

How does immutability enable time-travel?

A

Each move creates a new array without mutating old ones, so all previous board states are preserved and can be revisited.

20
Q

Why does handleClick return early in two cases?

A

if (calculateWinner(squares) || squares[i]) return — prevents overwriting a filled square and stops play after a win.

21
Q

How does the game determine whose turn it is?

A

xIsNext = currentMove % 2 === 0. X goes on even moves (0, 2, 4…), O on odd (1, 3, 5…).

22
Q

What does calculateWinner check?

A

All 8 winning lines (3 rows, 3 columns, 2 diagonals). If any line has three matching non-null values, that player wins.

23
Q

Describe the full click flow when a user clicks a square.

A

Square button onClick fires -> calls onSquareClick prop -> Board handleClick copies squares, sets X/O, calls setSquares -> Board and Squares re-render.

24
Q

What does lifting state up mean in this tutorial?

A

Moving shared state into a parent. It happens twice: Square -> Board (squares), then Board -> Game (history).

25
How is Game's history state structured?
An array of board snapshots: [[null x9], [board after move 1], ...]. Each entry is the full 9-element squares array.
26
What two state variables does the Game component hold?
history (array of board snapshots) and currentMove (index into history). xIsNext and currentSquares are derived.
27
What is the difference between stored state and derived state in this game?
Stored: history and currentMove (in useState). Derived: xIsNext (currentMove % 2 === 0), currentSquares (history[currentMove]).
28
How does jump to move work in time travel?
setCurrentMove(moveIndex) updates position. The displayed board is derived: currentSquares = history[currentMove].
29
Why does making a new move after jumping back discard future moves?
handlePlay slices history up to currentMove + 1 before appending, dropping all entries after the jump point.
30
How is the move history list rendered?
history.map((squares, move) => ...) transforms each snapshot into a
  • with a button calling jumpTo(move).
  • 31
    Why does each move history list item need a key prop?
    Keys let React identify which items changed. The move index works here because moves are never reordered or deleted.
    32
    Describe the final component hierarchy.
    Game (owns history + currentMove) -> Board (receives squares + onPlay, displays status) -> Square (receives value + onSquareClick, renders button).
    33
    What is a controlled component in this context?
    A component like Square that has no internal state and is fully controlled by its parent via props.
    34
    Why can handleClick access squares and setSquares?
    JavaScript closures: inner functions access variables defined in their enclosing scope (the Board function body).