我关注 the official reactjs创建示例应用程序的说明。我的节点版本是 6.9.0。
我创建了示例 react 应用程序,该应用程序应该根据官方网站使用以下说明显示一个空的井字游戏表:npm install -g create-react-app
create-react-app my-app
cd 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.
最佳答案
你应该创建一些组件/元素/,也许它的样式,然后调用ReactDOM
将您的组件渲染到底层 html,然后您将拥有它。
React
用于处理 JSX 和 React 组件的创建 ReactDOM
在您的简单情况下,将用于将创建的元素呈现给 dom。 见这里:https://reactjs.org/blog/2015/10/01/react-render-and-top-level-api.html
所以添加类似的东西
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
对于您的代码,如果在您的
index.html
中,您将得到一些东西有 id="root"
的元素里面 <body>
标签
关于javascript - 'React' 已定义但从未使用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980449/