Last active
September 24, 2018 01:50
-
-
Save zehnpaard/5accc7b6aec28c61e95e9e91367c18bc to your computer and use it in GitHub Desktop.
Reagent port of official React tutorial code upto "Lifting State Up" https://facebook.github.io/react/tutorial/tutorial.html#lifting-state-up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns react-tutorial.core | |
(:require | |
[reagent.core :as r])) | |
(defn Square [value click-fn] | |
[:button.square {:on-click click-fn} value]) | |
(defn Board [] | |
(let [squares (r/atom (apply vector (repeat 9 nil))) | |
handle-click #(swap! squares assoc % "X") | |
renderSquare (fn [i] | |
[Square (get @squares i) #(handle-click i)]) | |
status (r/atom "Next player: X")] | |
(fn [] | |
[:div | |
[:div.status @status] | |
[:div.board-row | |
[renderSquare 0] | |
[renderSquare 1] | |
[renderSquare 2]] | |
[:div.board-row | |
[renderSquare 3] | |
[renderSquare 4] | |
[renderSquare 5]] | |
[:div.board-row | |
[renderSquare 6] | |
[renderSquare 7] | |
[renderSquare 8]]]))) | |
(defn Game [] | |
[:div.game | |
[:div.game-board | |
[Board]] | |
[:div.game-info | |
[:div] | |
[:ol]]]) | |
(r/render | |
[Game] | |
(js/document.getElementById "container")) | |
(defn calculateWinner [squares] | |
(let [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]]] | |
(->> lines | |
(filter (fn [[a b c]] | |
(and | |
(not (nil? (get squares a))) | |
(= (get squares a) | |
(get squares b) | |
(get squares c))))) | |
first | |
first | |
(get squares)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Square extends React.Component { | |
render() { | |
return ( | |
<button className="square" onClick={() => this.props.onClick()}> | |
{this.props.value} | |
</button> | |
); | |
} | |
} | |
class Board extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
squares: Array(9).fill(null), | |
}; | |
} | |
handleClick(i) { | |
const squares = this.state.squares.slice(); | |
squares[i] = 'X'; | |
this.setState({squares: squares}); | |
} | |
renderSquare(i) { | |
return <Square value={this.state.squares[i]} onClick={() => this.handleClick(i)} />; | |
} | |
render() { | |
const status = 'Next player: X'; | |
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('container') | |
); | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment