Javascript 教程代码->';反应';使用JSX时必须在范围内
我是react.js新手,这是他们网站上教程中提供的用于制作tic-tac-toe游戏的文字代码。然而,当我尝试编译时,当使用JSX时,->“React”必须在范围内。我已经复制并粘贴了教程代码。我目前正在使用vim编辑器。如果有更好的编辑器使用,请建议Javascript 教程代码->';反应';使用JSX时必须在范围内,javascript,reactjs,Javascript,Reactjs,我是react.js新手,这是他们网站上教程中提供的用于制作tic-tac-toe游戏的文字代码。然而,当我尝试编译时,当使用JSX时,->“React”必须在范围内。我已经复制并粘贴了教程代码。我目前正在使用vim编辑器。如果有更好的编辑器使用,请建议 function Square(props) { return ( <button className="square" onClick={props.onClick}> {props.value}
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
class Board extends React.Component {
renderSquare(i) {
return (
<Square
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)} //onclick button
/>
);
}
render() {
return (
<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 {
constructor(props) {
super(props);
this.state = {
history: [
{
squares: Array(9).fill(null)
}
],
stepNumber: 0,
xIsNext: true
};
}
handleClick(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? "X" : "O";
this.setState({
history: history.concat([
{
squares: squares
}
]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext
});
}
jumpTo(step) {
this.setState({
stepNumber: step,
xIsNext: (step % 2) === 0
});
}
render() {
const history = this.state.history; //to store history of
//game
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
const moves = history.map((step, move) => {
const desc = move ?
'Go to move #' + move :
'Go to game start';
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});
let status;
if (winner) {
status = "Winner: " + winner;
} else {
status = "Next player: " + (this.state.xIsNext ? "X" : "O");
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={i => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
}
// ========================================
ReactDOM.render(<Game />, document.getElementById("root"));
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;
}
功能广场(道具){
返回(
{props.value}
);
}
类板扩展React.Component{
renderSquare(一){
返回(
this.props.onClick(i)}//onClick按钮
/>
);
}
render(){
返回(
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
);
}
}
类游戏扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
历史:[
{
正方形:数组(9)。填充(空)
}
],
步骤编号:0,
下一个:是的
};
}
handleClick(一){
const history=this.state.history.slice(0,this.state.stepNumber+1);
const current=history[history.length-1];
const squares=当前的.squares.slice();
if(计算平方数)| |平方[i]){
返回;
}
正方形[i]=this.state.xIsNext?“X”:“O”;
这是我的国家({
历史:history.concat([
{
正方形:正方形
}
]),
步骤编号:history.length,
xIsNext:!this.state.xIsNext
});
}
跳到(步骤){
这是我的国家({
步骤编号:步骤,
xIsNext:(步骤%2)==0
});
}
render(){
const history=this.state.history;//用于存储
//游戏
const current=history[this.state.stepNumber];
const winner=calculateWinner(当前平方);
常量移动=历史。映射((步骤,移动)=>{
const desc=移动?
“转到移动”#+移动:
“进入游戏开始”;
返回(
this.jumpTo(move)}>{desc}
);
});
让状态;
国际单项体育联合会(优胜者){
status=“Winner:”+Winner;
}否则{
status=“Next player:”+(this.state.xIsNext?“X”:“O”);
}
返回(
这个.handleClick(i)}
/>
{状态}
{moves}
);
}
}
// ========================================
render(,document.getElementById(“根”));
函数计算器(平方){
常量行=[
[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(设i=0;i
由于您使用的是class Game extends React.Component,因此需要对顶部文件中包含的组件的依赖关系。此外,如果ReactDOM.render调用位于同一文件中,则也必须包含该依赖项。
另外,我希望您在package.json文件中有React和ReactDOM依赖项
把这个放在文件的顶部
Import React from ‘react’;
Import ReactDOM from ‘react-dom’;
import-React from'React'
import的可能重复项应为小写,'React'
和'React-dom'
应为普通引号,而非智能引号。此外,我们还应添加import./index.css'
以及他们让我们添加的样式表。我不知道为什么这一切都被遗漏在教程中。