Using React Hooks to Build a Tic-Tac-Toe Game with Kent C. Dodds

Joel Hooks
InstructorJoel Hooks
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 3 years ago

To help accelerate our understanding of React Hooks, Kent is going to live-code a tic-tac-toe app from the ground up taking a Hooks first approach!

Hooks allow you to better separate your app's visual components from its state and functionality. Kent takes an accessibility and readability first approach to React development, so using hooks to separate your concerns makes your app easier to debug as well as reducing the mental and visual overhead when you are making sense of the code, making your code all around more maintainable.

  • useHooks.com

  • Data is static information, state changes over your component's lifetime

  • Reducers let you move logic from your component to your pure reducer function. Changes to state always go to the same function, it's great for debugging.

  • Reducer concepts are covered excellently in Dan Abramov's Getting Started with Redux course

Tony Catalfo
Tony Catalfo
~ 6 years ago

Sorry, that was a mess. const Board=()=>{ const [squares, setSquares]= useState(Array(9).fill(null)) const[xIsNext, setXisNext]=useState(true) const selectSquare=(square)=>{ const xOrO=(elm,i)=>{ if(i !==square || elm !==null)return elm return (xIsNext)? "X":"O" } let squaresCopy=squares.map(xOrO) // console.log('squaresCopy ',squaresCopy[square]='x') setSquares(squaresCopy) console.log(squares,squaresCopy) let status; if (calculateWinner(squaresCopy)){ status = "Winner: " + ((xIsNext) ? "X" : "O") }else{ status = "Next Player: " + ((xIsNext) ? "X" : "O") } console.log(status) setXisNext(x=>!x)

}

return (

<div>
  
  <div className='status'>{status}</div>
Markdown supported.
Become a member to join the discussionEnroll Today