freeCodeCamp/guide/chinese/certifications/front-end-libraries/react/render-a-class-component-to.../index.md

35 lines
917 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Render a Class Component to the DOM
localeTitle: 将类组件渲染到DOM
---
## 将类组件渲染到DOM
你的代码应该看起来像这样:
```javascript
class TypesOfVehicles extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h1>Types of Vehicles:</h1>
<Car />
<Motorcycle />
</div>
);
}
}
ReactDOM.render(<TypesOfVehicles />,'node-id')
```
ReactDOM.render语法可能有点棘手在传入类组件时需要使用三角括号。此外两个子组件在幕后声明如果您习惯于在代码编辑器中定义并在您面前可见的所有变量则可能会造成混淆。
### 暗示
* 使用document.getElementById'id')来获取目标节点
### 相关链接
* [渲染元素](https://reactjs.org/docs/rendering-elements.html)