1、 概述

一切都是模块 module。

React 是完全基于模块化的开发思路。整个项目是由多个小模块,通过 ESModule 语法组装在一起形成。在 vite 构建工具支持的环境中,每一个项目文件都会被看做是一个模块参与到项目的组成中去。

例如,我们可以使用 import 语法在 JS 模块中引入 css 文件与图片

app.tsx
                                        
1
import Logo from '/assets/images/logo.png '
2
import './index.css '

这样,index.css 文件就会被解析成为 app.tsx 的一部分影响元素样式。我们也可以在当前模块中使用 Logo 模块

app.tsx
                                        
1
< a href = "https://vite.dev " target = "_blank " >
2
< img src ={ Logo } className = "logo " alt = "Vite logo " />
3
</ a >

1、 插入到真实 DOM

所有的模块最终会组合成为一个大的模块 App.tsx ,通过 createRoot 插入到真实 DOM 中去,从而渲染出来内容。

index.html 中,我们会提前编写好一个项目根节点

index.html
                                        
1
<! doctype html >
2
< html lang = "en " >
3
< head >
4
< meta charset = "UTF-8 " />
5
< link rel = "icon " type = "image/svg+xml " href = "/vite.svg " />
6
< meta name = "viewport " content = "width=device-width, initial-scale=1.0 " />
7
< title >Vite + React + TS </ title >
8
</ head >
9
< body >
10
< div id = "root " ></ div >
11
< script type = "module " src = "/src/main.tsx " ></ script >
12
</ body >
13
</ html >

然后在 src 的入口文件 main.tsx 中,将组合好的内容渲染到 root 节点中

                                        
1
import { StrictMode } from 'react '
2
import { createRoot } from 'react-dom/client '
3
import './index.css '
4
import App from './App.tsx '
5
6
createRoot (document. getElementById ( 'root ' ) ! ). render (
7
< StrictMode >
8
< App />
9
</ StrictMode > ,
10
)

3、 使用函数创建一个组件

在 React 中,创建一个组件非常简单,只需要在函数中返回一段 JSX 即可。JSX 的基础知识我们会专门花一个章节来学习。它是一种与 html 非常相似的语法糖,原生的 JavaScript 并不支持这种语法,构建工具会负责专门解析这种语法,从而让其在构建工具的环境之下得以支持。

hello.tsx
                                        
1
export default function Hello () {
2
return (
3
< div >hello world </ div >
4
)
5
}

这就是一个自定义组件。注意:在 React 中,自定义组件的函数名必须要大写

创建组件还有许多知识点需要学习,我们会在后续的章节中详细分析。

这样声明之后,我们就可以在别的组件模块中引入该组件,并将其当做元素标签来使用。

app.tsx
                                        
1
import Hello from './hello '
2
3
function App () {
4
...
5
6
return (
7
< div >
8
< Hello />
9
...
10
</ div >
11
)
12
}

我们可以看到,React 组件的学习,与 JSX 语法息息相关,因此,我们需要专门花一个章节来学习它。

下一篇
7、JSX
专栏首页
到顶
专栏目录