728x90
기본 설정 세팅
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class App extends React.Component{
render(){
return(
<div>
</div>
)
}
}
ReactDOM.render(
<App />,
document.querySelector('#root')
)
</script>
</body>
</html>
코드 구현
<body>
<div id="root"></div>
<script type="text/babel">
// Square
// Board
// game
class Square extends React.Component{
state = {
value:null,
}
render(){
return(
<button className="square"
onClick={ ()=>{ this.props.onClick() } }
>
{this.props.value}
</button>
)
}
}
class Board extends React.Component{
state = {
squares:Array(9).fill(null),
xIsNext:true,
}
handleClick = (i)=>{
const squares = [...this.state.squares]
squares[i] = this.state.xIsNext ? "X" : "O"
this.setState({
squares,
xIsNext:!this.state.xIsNext
})
}
renderSquare = i =>{
return <Square
onClick={ ()=>{this.handleClick(i)}}
value = {this.state.squares[i]}
/>
}
render(){
return(
<div>
<div className="status"></div>
<div className="board-rows">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-rows">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-rows">
{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>
)
}
}
ReactDOM.render(
<Game />,
document.querySelector('#root')
)
</script>
</body>
css
body {
font: 14px "Century Gothic", Futura, sans-serif;
margin: 20px;
}
ol, ul {
padding-left: 30px;
}
.board-row:after {
clear: both;
content: "";
display: table;
}
.status {
margin-bottom: 10px;
}
.square {
background: #fff;
border: 1px solid #999;
float: left;
font-size: 24px;
font-weight: bold;
line-height: 34px;
height: 34px;
margin-right: -1px;
margin-top: -1px;
padding: 0;
text-align: center;
width: 34px;
}
.square:focus {
outline: none;
}
.kbd-navigation .square:focus {
background: #ddd;
}
.game {
display: flex;
flex-direction: row;
}
.game-info {
margin-left: 20px;
}
다음 게시물
728x90
'프론트엔드 > React' 카테고리의 다른 글
[React] Webpack(웹팩)이란? 웹팩 설치 및 사용법 (0) | 2022.04.18 |
---|---|
[React] 틱택토게임(2) 승자정하기 (0) | 2022.04.15 |
[React] 타자 입력 시마다 value값 변하는 form만들기 (0) | 2022.04.14 |
[React] 버튼 누를 때마다 숫자 증가, 감소하는 카운터 만들기 (0) | 2022.04.13 |
[React] 버튼 클릭할 때마다 버튼 글씨 바꾸기 (0) | 2022.04.13 |