๐Ÿ“’[React ์ž์Šต์„œ] 4. ์ฝ”๋“œ์ถ”๊ฐ€2

Square ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ”๊พธ๋ฉด ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๊ฒฐํ•ด์ง. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋Š” props ๊ฐ์ฒด๋กœ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜์ž.

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

O, X๋ฅผ ํ‘œ์‹œํ•˜๋„๋ก ํ•˜์ž (boolean ๊ฐ’ ์ด์šฉ)

class Board extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      squares: Array(9).fill(null),
      xIsNext:true,                 ........(1)
    };
  }
   handleClick(i){
    const squares=this.state.squares.slice();
    if (squares[i]){return;}         .......(*)
    squares[i]=this.state.xIsNext ? 'X':'O';
    this.setState({
      squares:squares,
      xIsNext: !this.state.xIsNext,  ........(2)
    });
  }
  ...
  (์ค‘๋žต)
  ...
   render() {                      ........(3)
    const status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O'); 

(1) xIsNext๋ฅผ state์— ์ถ”๊ฐ€ํ•˜์—ฌ ์ฒซ๋ฒˆ์งธ user์˜ ํ„ด(O)์ธ์ง€ ๋‘๋ฒˆ์งธ user์˜ ํ„ด(X)์ธ์ง€ ํ™•์ธ
(2) ํ˜„์žฌ ํ„ด์ธ user๊ฐ€ ์„ ํƒํ•œ ์˜์—ญ์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•จ.
(3) ๋‹ค์Œ ํ„ด์˜ user์„ ํ™”๋ฉด์— ํ‘œ์‹œ.
(*) ๋ˆ„๊ตฐ๊ฐ€ ํด๋ฆญํ–ˆ๋‹ค๋ฉด return (ํด๋ฆญ๋˜์ง€ ์•Š์Œ.)



๊ฐ€๋กœ ์„ธ๋กœ ๋Œ€๊ฐ์„  ํ•œ ์ค„์„ ํ•œ user๊ฐ€ ๋‹ค ์„ ํƒํ•˜๋ฉด ์ด๊น€

  • ์Šน์ž ๊ฒฐ์ • ํ•จ์ˆ˜
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;
}
  • ์Šน์ž ๊ฒฐ์ • ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ
//Board์˜ renderํ•จ์ˆ˜
  render() {
    const winner = calculateWinner(this.state.squares);
    let status;
    if (winner) {
      status = 'Winner: ' + winner;
    } else {
      status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
    }

๋™์ž‘์— ๋Œ€ํ•œ ๊ธฐ๋ก ์ €์žฅํ•˜๊ธฐ-(๋ฐœ์ „)->๋’ค๋กœ ๋˜๋Œ์•„๊ฐ€๊ธฐ

์ด์ „ ๋™์ž‘์— ๋Œ€ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ฃผ๋ ค๋ฉด ์ตœ์ƒ์œ„ ๋‹จ๊ณ„์˜ Game ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. history๋ฅผ ์ด์šฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ƒ์œ„ ๋‹จ๊ณ„ Game ์ปดํฌ๋„ŒํŠธ์— history state๋ฅผ ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค.

history state๋ฅผ Game ์ปดํฌ๋„ŒํŠธ์— ๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ Board ์ปดํฌ๋„ŒํŠธ์—์„œ squares state๋ฅผ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

slice()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋งค ๋™์ž‘ ์ดํ›„์— squares ๋ฐฐ์—ด์˜ ์ƒˆ๋กœ์šด ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค์—ˆ๊ธฐ์—, ๊ณผ๊ฑฐ์˜ squares ๋ฐฐ์—ด์˜ ๋ชจ๋“  ๋ฒ„์ „์„ ์ €์žฅํ•˜๊ณ  ์ด๋ฏธ ์ง€๋‚˜๊ฐ„ ์ฐจ๋ก€๋ฅผ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ณผ๊ฑฐ์˜ squares ๋ฐฐ์—ด๋“ค์„ history๋ผ๋Š” ๋‹ค๋ฅธ ๋ฐฐ์—ด์— ์ €์žฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. history ๋ฐฐ์—ด์€ ์ฒซ ๋™์ž‘๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋™์ž‘๊นŒ์ง€ ๋ชจ๋“  ๊ฒŒ์ž„ํŒ์˜ ์ƒํƒœ๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค.

Game์— history์ €์žฅ.


(1) Board์— ์žˆ๋˜ state๋ฅผ Game์œผ๋กœ ๊ฐ€์ ธ์˜ด
(1.1) Game์ž์‹ ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” history
(1.2) ๋ฐ”๋กœ ์ง์ „์˜ history
(1.3) ํ˜„์žฌ user๊ฐ€ ์„ ํƒํ•˜๋ฉด ๋ฐ”๋€ ๊ฒฐ๊ณผ squares๋ฅผ Game ์ž์‹ ์˜ history์— concat.

Board๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ Game์—๊ฒŒ props์„ ํ†ตํ•ด ํ˜„์žฌ squares๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.

Board๋Š” Game์—๊ฒŒ์„œ ์ „๋‹ฌ ๋ฐ›์€ props๊ฐ์ฒด์˜ squares[i](squares์˜ ํ•ด๋‹น ์นธ์˜ ๊ฐ’)๋ฅผ ์ž์‹ ์ปดํฌ๋„ŒํŠธ Square์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

์ด๋กœ์จ ์นธ ํ•˜๋‚˜๊ฐ€ ํ™”๋ฉด์— ํ‘œ์‹œ๋œ๋‹ค.

ํŠน์ • ๊ณผ๊ฑฐ๋กœ์˜ ์ด๋™


์œ„์™€ ๊ฐ™์ด ์“ฐ๋ฉด,,,, ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

๊ฒฝ๊ณ  ๋ฐฐ์—ด์ด๋‚˜ ์ดํ„ฐ๋ ˆ์ดํ„ฐ์˜ ์ž์‹๋“ค์€ ๊ณ ์œ ์˜ โ€œkeyโ€ prop์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋ฆฌ์ŠคํŠธ ์•„์ดํ…œ์— key prop์„ ์ง€์ •ํ•˜์—ฌ ๊ฐ ์•„์ดํ…œ์ด ๋‹ค๋ฅธ ์•„์ดํ…œ๋“ค๊ณผ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ..!

<li key={user.id}>{user.name}: {user.taskCount} tasks left</li>

๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด key๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค˜์•ผ ํ•œ๋‹ค.

1. Game ์ปดํฌ๋„ŒํŠธ์˜ state์— stepNumber๋ฅผ ์ถ”๊ฐ€.

ํ˜„์žฌ ์ง„ํ–‰ ์ค‘์ธ ๋‹จ๊ณ„ ํ‘œ์‹œ.

2. Game ์ปดํฌ๋„ŒํŠธ์— jumpToํ•จ์ˆ˜ ์ถ”๊ฐ€.


xIsNext๋Š” x,o,x,o...์ˆœ์„œ์ด๋ฏ€๋กœ

3. ๊ณผ๊ฑฐ๋กœ ๊ฐ„ ํ›„ ์ƒˆ๋กœ์šด ์ด๋™์„ ํ•œ๋‹ค๋ฉด ๋ฏธ๋ž˜์˜ ๊ธฐ๋ก์„ ์—†์•ค๋‹ค.

4. render


๋ฐ”๋กœ ์ง์ „์˜ ๊ฐ’์—์„œ history.length-1 this.state.stepNumber ๋กœ ๋ฐ”๊ฟˆ.

์ตœ์ข…๊ฒฐ๊ณผ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ