Commit 57b10875 authored by Matt 高萌's avatar Matt 高萌

分离出组件并增加颜色点击变化

parent 34a8556a
This diff is collapsed.
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
"dependencies": { "dependencies": {
"dva": "^2.4.1", "dva": "^2.4.1",
"react": "^16.2.0", "react": "^16.2.0",
"react-dom": "^16.2.0" "react-dom": "^16.2.0",
"styled-components": "^4.3.2"
}, },
"devDependencies": { "devDependencies": {
"babel-plugin-dva-hmr": "^0.3.2", "babel-plugin-dva-hmr": "^0.3.2",
......
import React from "react"
import styled from "styled-components"
const SquareButton = styled.button`
background: ${(props) => props.backgroundColor};
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;
`
function Square(props) {
return (
<SquareButton backgroundColor={props.value ? (props.value === "X" ? "blue" : "red") : "#fff"} onClick={props.onClick}>
{props.value}
</SquareButton>
);
}
export default Square;
...@@ -7,7 +7,8 @@ export default { ...@@ -7,7 +7,8 @@ export default {
} }
], ],
stepNumber: 0, stepNumber: 0,
xIsNext: true xIsNext: true,
backgroundColor: null
}, },
reducers: { reducers: {
...@@ -21,7 +22,8 @@ export default { ...@@ -21,7 +22,8 @@ export default {
return { return {
history: history.concat([{ squares: squares }]), history: history.concat([{ squares: squares }]),
stepNumber: history.length, stepNumber: history.length,
xIsNext: !state.xIsNext xIsNext: !state.xIsNext,
backgroundColor: state.xIsNext ? "blue" : "red"
} }
}, },
......
import React from 'react'; import React from 'react';
import { connect } from 'dva'; import { connect } from 'dva';
import styles from "./index.css"; import styles from "./index.css";
import Square from "../../components/Square"
function Square(props) {
return (
<button className={styles.square} onClick={props.onClick}>
{props.value}
</button>
);
}
function calculateWinner(squares) { function calculateWinner(squares) {
...@@ -89,7 +81,7 @@ class TicTacToe extends React.Component { ...@@ -89,7 +81,7 @@ class TicTacToe extends React.Component {
<div className={styles.game}> <div className={styles.game}>
<div className={styles['game-board']}> <div className={styles['game-board']}>
{squares.map((item, key) => ( {squares.map((item, key) => (
<Square key={key} index={key} value={item} onClick={() => this.nextStep(key)} /> <Square key={key} backgroundColor={ticTacToe.backgroundColor} value={item} onClick={() => this.nextStep(key)} />
))} ))}
</div> </div>
<div className={styles['game-info']}> <div className={styles['game-info']}>
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment