简体   繁体   English

React:'React' 未定义 no-undef

[英]React: 'React' is not defined no-undef

I am trying to follow the React tutorial on reactjs.org.我正在尝试遵循 reactjs.org 上的 React 教程。 I installed npm and set up my-app.我安装了 npm 并设置了我的应用程序。 However now that I have created my index.js file I am getting the error但是现在我已经创建了我的 index.js 文件,我收到了错误

src\App.js
  Line 9:21:   'React' is not defined     no-undef
  Line 42:20:  'React' is not defined     no-undef
  Line 124:1:  'ReactDOM' is not defined  no-undef

I've got a feeling that it maybe didn't install react properly or something but I really have no clue and haven't been able to find any solutions after searching for the last half an hour.我有一种感觉,它可能没有正确安装 react 或其他东西,但我真的不知道,在搜索了最后半小时后也找不到任何解决方案。 Does anyone know what the issue is?有谁知道问题是什么?

index.js: index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

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)}
      />
    );
  }

  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;
    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;
}

I think you did not install npm packages.我认为您没有安装 npm 软件包。

Check if you have a folder named node_module at the root of your project.检查项目根目录下是否有一个名为node_module的文件夹。 If not write in your console at the root of your project npm install which will install packages from npm required to react to the work.如果不在项目根目录的控制台中写入npm install ,它将安装 npm 所需的包以对工作做出反应。

If after npm install it still doesn't work check if you have in your package.json a line refering to react.如果在npm install之后它仍然无法工作,请检查您的 package.json 中是否有一个表示反应的行。 In fact npm install will install packages reading your package.json实际上npm install将安装读取您的 package.json 的软件包

I pasted your code here and it works fine https://codesandbox.io/s/aged-waterfall-nq36u?file=/src/index.js我在这里粘贴了你的代码,它工作正常https://codesandbox.io/s/aged-waterfall-nq36u?file=/src/index.js

It looks to me like you need to run npm i in the root directory of your project.在我看来,您需要在项目的根目录中运行npm i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM