“React”已定义但从未使用过

新手上路,请多包涵

我正在按照 官方的 reactjs 说明创建示例应用程序。我的节点版本是 6.9.0。

我创建了示例反应应用程序,它应该根据官方网站使用以下说明显示一个空的井字游戏表:

npm install -g create-react-app

create-react-app my-app

cd my-app

更改为 my-app 目录

按照指示删除源目录中的默认文件。现在我的 index.js 看起来像这样

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

然后我跑了 yarn start

但我所看到的只是空白屏幕,没有井字游戏桌。并在控制台中发出警告说

Compiled with warnings.

./src/index.js
  Line 1:  'React' is defined but never used     no-unused-vars
  Line 2:  'ReactDOM' is defined but never used  no-unused-vars

Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.

原文由 user3576036 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 569
1 个回答

您错过了第 4 步和第 5 步的最后部分:

  1. 使用 此 CSS 代码 在 src/ 文件夹中添加一个名为 index.css 的文件。
  2. 使用 此 JS 代码 在 src/ 文件夹中添加一个名为 index.js 的文件。

索引.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;
}

索引.js

 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')
);

原文由 CYMA 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题