我在本地安装了最新的React JS:
npm install -g create-react-app
create-react-app hello-world
cd hello-world
npm start
App.js:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import ReactDOM from 'react-dom';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<div id="root"></div>
<div id="test"></div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
ReactDOM.render(
<h1>Hello, world Root!</h1>,
document.getElementById('root')
);
ReactDOM.render(
<h1>Hello, world Test!</h1>,
document.getElementById('test')
);
export default App;
index.html:
<body>
<div id="root"></div>
<div id="test"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start`.
To create a production bundle, use `npm run build`.
-->
</body>
问题在于我只能打印出 Hello, world Test! 而无法打印出 Hello, world Root! 尽管没有出现任何错误,这意味着它是一个 DOM 元素。我也进行了检查,但仍然没有渲染出来。
另外还有一件事,我在 index.html 中添加了 ID 为 'test' 的 div,这是一个不错的方法来添加 div。
我对 React 不熟悉,请帮忙。