1.在cmd查询node.js,node -v ,查询npm,npm -v;如果没有请安装node.js
2.按照 Create React App 安装指南创建一个新的项目;
npx create-react-app my-app cd my-app npm start
3.删除掉新项目中 src/ 文件夹下的所有文件。
cd my-app cd src # 如果你使用 Mac 或 Linux: rm -f * # 如果你使用 Windows: del * # 然后回到项目文件夹 cd ..
- 在
src/文件夹中创建一个名为index.css的文件,并拷贝这些 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;
}
- 在
src/文件夹下创建一个名为index.js的文件
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class Square extends React.Component {
render() {
return (
<button className="square">
{/* TODO */}
</button>
);
}
}
class Board extends React.Component {
renderSquare(i) {
return <Square />;
}
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('root')
);
- 拷贝以下三行代码到
src/文件夹下的index.js文件的顶部
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css';
启动:npm start

本文指导如何在CMD中检查Node.js和npm的版本,以及在没有安装的情况下进行安装。接着,通过CreateReactApp快速创建一个新的React项目,并删除默认的src文件夹内容。然后,创建并添加必要的CSS样式文件`index.css`和JS组件文件`index.js`,为一个简单的井字游戏布局打下基础。最后,启动项目并开始开发。
1201

被折叠的 条评论
为什么被折叠?



