react组件入门

embedded/2024/9/24 18:56:22/

react应用程序就是由一个个组件搭建而成。组件有类组件和函数组件两种。

我们之前使用create-react-app创建了app,src下放的就是我们应用的源代码,我们基于这些已生成的文件,来学习和验证组件。

类组件

这里我们创建PostList.js更改这个app。

javascript">// src/PostList.js
// 必须import和extends react的component
import React from "react";
// 组件标签的名字首字母必须大写,这里必须采用驼峰书写方式
class PostList extends React.Component {// 类组件内部必须有一个render,render方法返回代表该数组件UI的// React元素render() {return {<div>post list<ul><li> post 1 </li><li> post 2 </li><li> post 3 </li></ul></div>  };}
}
// 将PostList作为默认模块导出,从而可以再其他js文件中导入PostList使用
export default PostList;

这个代码段定义了一个博客列表。 index.js是默认程序的入口。我们实现以下变更:

diff --git a/src/index.js b/src/index.js
index d563c0f..8dd4562 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,13 +1,14 @@import React from 'react';import ReactDOM from 'react-dom/client';import './index.css';
-import App from './App';
+//import App from './App';
+import PostList from './PostList'import reportWebVitals from './reportWebVitals';const root = ReactDOM.createRoot(document.getElementById('root'));root.render(<React.StrictMode>
-    <App />
+    <PostList /></React.StrictMode>);

我们刷新localhost:3000,就可以看到更新了。

函数组件

function ComponentName就是函数组件。我们在类组件基础上添加函数组件。

javascript">diff --git a/src/index.js b/src/index.js
index d563c0f..ac9a9d4 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,13 +1,27 @@import React from 'react';import ReactDOM from 'react-dom/client';import './index.css';
-import App from './App';
+//import App from './App';
+import PostList from './PostList'import reportWebVitals from './reportWebVitals';+function PostList1() {
+  return (
+      <div>
+        post list 1
+        <ul>
+          <li> post 4 </li>
+          <li> post 5 </li>
+          <li> post 6 </li>
+        </ul>
+      </div>
+    );
+}const root = ReactDOM.createRoot(document.getElementById('root'));root.render(<React.StrictMode>
-    <App />
+    <PostList />
+    <PostList1 /></React.StrictMode>);

刷新app,我们看到,此时的界面为:

我们这里举的例子是最简单的类组件和函数组件,后面我们陆续介绍二者丰富的用法。


http://www.ppmy.cn/embedded/116216.html

相关文章

oracle dblink的使用并举例

Oracle Database Link&#xff08;DB Link&#xff09;是Oracle提供的一种功能&#xff0c;允许你在一个数据库中直接访问另一个远程或本地数据库的对象&#xff08;如表、视图、序列等&#xff09;。DB Link的设置简化了跨数据库操作&#xff0c;使得数据的集成和同步变得更加…

windows桌面管理软件推荐:一键整理桌面!美化电脑桌面小助手!

windows桌面管理软件推荐来咯&#xff01;在繁忙的工作和生活中&#xff0c;一个整洁、有序的电脑桌面不仅能提升工作效率&#xff0c;还能带来愉悦的视觉体验。然而&#xff0c;随着文件的增多&#xff0c;桌面往往变得杂乱无章。幸运的是&#xff0c;市面上有许多优秀的Windo…

Spring学习指南

文章目录 Spring是什么Spring 的诞生与发展Spring 的狭义和广义广义的 Spring:Spring 技术栈狭义的 Spring:Spring FrameworkSpring Framework 的特点方便解耦,简化开发方便集成各种优秀框架降低 Java EE API 的使用难度方便程序的测试AOP 编程的支持声明式事务的支持为什么…

Java List sort() 排序

sort是java.util.List接口的默认方法。 List的排序方法在Java 8中被引入。 排序方法接受比较器作为参数&#xff0c;并根据指定的比较器对这个列表进行排序。 default void sort(Comparator<? super E> c) 示例代码&#xff1a; import java.text.Collator; import …

大模型LLM对话模拟器Dialogue Simulator Visualization可视化工具

伴随着生成式人工智能技术发展&#xff0c;进2年涌现出大语言模型LLM/Agent系统/AI推理等众多方向的技术项目和论文。其中对话系统&#xff0c;智能体交互是用户通过UX界面和AI系统进行交互&#xff0c;这种交互有时候也是多模态&#xff08;用户输入文字/语音/图像&#xff09…

达梦-华为鲲鹏ARM架构下性能测试最佳实践

一、测试综述 1.1 测试目的 本次测试的目的是验证达梦数据库&#xff0c;在鲲鹏服务器下&#xff0c;不同服务器参数基于sysbench性能压力测试的表现。本次参数是根据为华为鲲鹏arm服务器调优十板斧内建议值调整 成长地图-鲲鹏开发套件开发文档-鲲鹏社区 1.2 通用指标 指标…

SQL_UNION

在 SQL 中使用 UNION 操作符时&#xff0c;被联合的两个或多个 SELECT 语句的列数必须相同&#xff0c;并且相应的列数据类型也需要兼容。这是因为 UNION 操作符会将结果组合成单个结果集&#xff0c;每个 SELECT 语句的结果行将按顺序放置在结果集中。 例如&#xff0c;如果你…

阿里巴巴新推出Java版AI 应用开发框架-Spring AI Alibaba

[Spring AI Alibaba 是一款 Java 语言实现的 AI 应用开发框架&#xff0c;旨在简化 Java AI 应用程序开发&#xff0c;让 Java 开发者像使用 Spring 开发普通应用一样开发 AI 应用。Spring AI Alibaba 基于 Spring AI 开源项目构建&#xff0c;默认提供阿里云基础模型服务、开源…