百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

React v19 稳定版发布!全新功能详解

zhezhongyun 2025-03-24 22:35 63 浏览

React 团队于 2024 年 12 月 5 日正式发布了 React v19 稳定版,并在 npm 上可用。此次更新带来了许多令人期待的新特性和改进,开发者们可以通过 React 19 升级指南获取详细的升级步骤和注意事项。本文将对 React 19 的新功能进行概述,并介绍如何在项目中应用这些特性。

React v19 新特性

Actions 和异步过渡

在 React 应用中,数据更新和状态变更是常见的场景。React 19 引入了 Actions 概念,通过支持异步函数,简化了处理待定状态、错误、乐观更新以及表单的逻辑。例如,新的 useActionState 钩子和

Actions 的结合,使得表单处理更加简洁高效。

function ChangeName({ name, setName }) {
  const [error, submitAction, isPending] = useActionState(
    async (previousState, formData) => {
      const error = await updateName(formData.get("name"));
      if (error) {
        return error;
      }
      redirect("/path");
      return null;
    },
    null,
  );

  return (
    
      
      
      {error && 

{error}

} ); }

新钩子与 API

  • useActionState: 简化了 Actions 的常见应用场景。
  • useOptimistic: 提供乐观更新功能,允许在异步请求进行时即刻更新 UI。
  • use: 支持在渲染时读取资源和上下文,使得在条件中使用 Context 成为可能。

React DOM 静态 API

React 19 引入了 prerenderprerenderToNodeStream 两个新的 API,用于改进静态 HTML 生成,支持流环境如 Node.js Streams 和 Web Streams。

服务器组件与服务器操作

React 服务器组件允许在构建时或每次请求时预先渲染组件,为开发者提供了新的选择。同时,服务器操作允许客户端组件调用在服务器上执行的异步函数,提升了客户端与服务器的协作能力。

改进与兼容性

  • Ref 作为属性:函数组件现在可以直接通过属性访问 ref,不再需要 forwardRef
  • 文档元数据支持:React 19 原生支持在组件中渲染 </span></span><span style='color: #333333; --tt-darkmode-color: #A3A3A3;'>、</span><span style='color: #333333; --tt-darkmode-color: #333333;'><span style='background-color: #F3F4F4; --tt-darkmode-bgcolor: #BDBEBE;'><meta></span></span><span style='color: #333333; --tt-darkmode-color: #A3A3A3;'> 和 </span><span style='color: #333333; --tt-darkmode-color: #333333;'><span style='background-color: #F3F4F4; --tt-darkmode-bgcolor: #BDBEBE;'><link></span></span><span style='color: #333333; --tt-darkmode-color: #A3A3A3;'> 标签,自动提升至文档的 </span><span style='color: #333333; --tt-darkmode-color: #333333;'><span style='background-color: #F3F4F4; --tt-darkmode-bgcolor: #BDBEBE;'><head></span></span><span style='color: #333333; --tt-darkmode-color: #A3A3A3;'>。</span></li><li style='text-align: left;' data-track='17'><span style='color: #333333; --tt-darkmode-color: #A3A3A3;'>样式表支持:通过声明优先级管理样式表的插入顺序,确保样式在依赖内容显示前加载完成。</span></li><li style='text-align: left;' data-track='18'><span style='color: #333333; --tt-darkmode-color: #A3A3A3;'>异步脚本支持:支持在组件树中任意位置渲染异步脚本,确保不会重复加载。</span></li></ul><h1 class='pgc-h-arrow-right' data-track='19'>升级指南</h1><p style='text-align: left;' data-track='20'><span style='color: #333333; --tt-darkmode-color: #A3A3A3;'>开发者可以参考 React 19 升级指南,获取详细的升级步骤和破坏性变更列表,以确保项目顺利过渡到最新版本。</span></p><p style='text-align: left;' data-track='21'><span style='color: #333333; --tt-darkmode-color: #A3A3A3;'>React v19 的发布为开发者带来了更强大的功能和更高效的开发体验,鼓励大家尽快尝试这些新特性,提升应用的响应速度和用户体验。详细信息请参见官方文档及相关技术文章。</span></p></div> <div class="clearfix mb10"> <div class="share fr"> <div class="social-share mb20 ta-c" data-initialized="true"> <a href="#" class="social-share-icon iconfont icon-weibo"></a> <a href="#" class="social-share-icon iconfont icon-qq"></a> <a href="#" class="social-share-icon iconfont icon-wechat"></a> <a href="#" class="social-share-icon iconfont icon-qzone"></a> </div> <script src="http://www.zhezhongyun.com/zb_users/theme/tx_hao/script/social-share.min.js"></script> </div> <div class="info-tag"> <a href="http://www.zhezhongyun.com/tags-171.html" title="查看更多标签内容" rel="tag" target="_blank">标签</a> </div> </div> <div class="info-next"> <ul class="row"> <li class="col-12 col-m-24 mb10">上一篇:<a href="http://www.zhezhongyun.com/post/2969.html" title="9个小细节帮你优化CSS代码(解决css js性能优化)">9个小细节帮你优化CSS代码(解决css js性能优化)</a></li> <li class="col-12 col-m-24 ta-r mb10">下一篇:<a href="http://www.zhezhongyun.com/post/2971.html" title="怎么避免网站中出现 index.html(怎么避免网站提示的危险)">怎么避免网站中出现 index.html(怎么避免网站提示的危险)</a></li> </ul> </div> </div> <h2 class="tx-title">相关推荐</h2> <div class="home-news"> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5190.html" title="perl基础——循环控制_principle循环" class="f-black" target="_blank">perl基础——循环控制_principle循环</a></dt> <dd class="news-txt"> <p class="f-gray f-13">在编程中,我们往往需要进行不同情况的判断,选择,重复操作。这些时候我们需要对简单语句来添加循环控制变量或者命令。if/unless我们需要在满足特定条件下再执行的语句,可以通过if/unle...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5189.html" title="CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅" class="f-black" target="_blank">CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅</a></dt> <dd class="news-txt"> <p class="f-gray f-13">CHAPTER1TheThreePresentsofD'ArtagnantheElderCHAPTER2TheAntechamber...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5188.html" title="CHAPTER 5 The King&#39;S Musketeers and the Cardinal&#39;S Guards 第五章 国王的火枪手和红衣主教的卫士" class="f-black" target="_blank">CHAPTER 5 The King&#39;S Musketeers and the Cardinal&#39;S Guards 第五章 国王的火枪手和红衣主教的卫士</a></dt> <dd class="news-txt"> <p class="f-gray f-13">CHAPTER3TheAudienceCHAPTER5TheKing'SMusketeersandtheCardinal'SGuard...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5187.html" title="CHAPTER 3 The Audience 第三章 接见" class="f-black" target="_blank">CHAPTER 3 The Audience 第三章 接见</a></dt> <dd class="news-txt"> <p class="f-gray f-13">CHAPTER3TheAudienceCHAPTER3TheAudience第三章接见M.DeTrévillewasatt...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5186.html" title="别搞印象流!数据说明谁才是外线防守第一人!" class="f-black" target="_blank">别搞印象流!数据说明谁才是外线防守第一人!</a></dt> <dd class="news-txt"> <p class="f-gray f-13">来源:Reddit译者:@assholeeric编辑:伯伦WhoarethebestperimeterdefendersintheNBA?Here'sagraphofStea...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5185.html" title="V-Day commemorations prove anti-China claims hollow" class="f-black" target="_blank">V-Day commemorations prove anti-China claims hollow</a></dt> <dd class="news-txt"> <p class="f-gray f-13">People'sLiberationArmyhonorguardstakepartinthemilitaryparademarkingthe80thanniversary...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5184.html" title="EasyPoi使用_easypoi api" class="f-black" target="_blank">EasyPoi使用_easypoi api</a></dt> <dd class="news-txt"> <p class="f-gray f-13">EasyPoi的主要特点:1.设计精巧,使用简单2.接口丰富,扩展简单3.默认值多,writelessdomore4.springmvc支持,web导出可以简单明了使用1.easypoi...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5183.html" title="关于Oracle数据库12c 新特性总结_oracle数据库12514" class="f-black" target="_blank">关于Oracle数据库12c 新特性总结_oracle数据库12514</a></dt> <dd class="news-txt"> <p class="f-gray f-13">概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5182.html" title="【开发者成长】JAVA 线上故障排查完整套路!" class="f-black" target="_blank">【开发者成长】JAVA 线上故障排查完整套路!</a></dt> <dd class="news-txt"> <p class="f-gray f-13">线上故障主要会包括CPU、磁盘、内存以及网络问题,而大多数故障可能会包含不止一个层面的问题,所以进行排查时候尽量四个方面依次排查一遍。同时例如jstack、jmap等工具也是不囿于一个方面的问题...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5181.html" title="使用 Python 向多个地址发送电子邮件" class="f-black" target="_blank">使用 Python 向多个地址发送电子邮件</a></dt> <dd class="news-txt"> <p class="f-gray f-13">在本文中,我们将演示如何使用Python编程语言向使用不同电子邮件地址的不同收件人发送电子邮件。具体来说,我们将向许多不同的人发送电子邮件。使用Python向多个地址发送电子邮件Python...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5180.html" title="提高工作效率的--Linux常用命令,能够决解95%以上的问题" class="f-black" target="_blank">提高工作效率的--Linux常用命令,能够决解95%以上的问题</a></dt> <dd class="news-txt"> <p class="f-gray f-13">点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf为什么要学习Linux命令?1、因为Li...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5179.html" title="linux常用系统命令_linux操作系统常用命令" class="f-black" target="_blank">linux常用系统命令_linux操作系统常用命令</a></dt> <dd class="news-txt"> <p class="f-gray f-13">系统信息arch显示机器的处理器架构dmidecode-q显示硬件系统部件-(SMBIOS/DMI)hdparm-i/dev/hda罗列一个磁盘的架构特性hdparm-tT/dev/s...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5178.html" title="小白入门必知必会-PostgreSQL-15.2源码编译安装" class="f-black" target="_blank">小白入门必知必会-PostgreSQL-15.2源码编译安装</a></dt> <dd class="news-txt"> <p class="f-gray f-13">一PostgreSQL编译安装1.1下载源码包在PostgreSQL官方主页https://www.postgresql.org/ftp/source/下载区选择所需格式的源码包下载。cd/we...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5177.html" title="Linux操作系统之常用命令_linux系统常用命令详解" class="f-black" target="_blank">Linux操作系统之常用命令_linux系统常用命令详解</a></dt> <dd class="news-txt"> <p class="f-gray f-13">Linux操作系统一、常用命令1.系统(1)系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本dmidecode-q显示硬件系...</p> </dd> </dl> <dl class="news-box clearfix pd20 "> <dt class="f-18 mb10"><a href="http://www.zhezhongyun.com/post/5176.html" title="linux网络命名空间简介_linux 网络相关命令" class="f-black" target="_blank">linux网络命名空间简介_linux 网络相关命令</a></dt> <dd class="news-txt"> <p class="f-gray f-13">此篇会以例子的方式介绍下linux网络命名空间。此例中会创建两个networknamespace:nsa、nsb,一个网桥bridge0,nsa、nsb中添加网络设备veth,网络设备间...</p> </dd> </dl> </div> </div> <div class="side-box col-6 col-m-24 col2-"> <dl class="side-hot"> <dt>一周热门</dt> <dd> <ul> <li> <a href="http://www.zhezhongyun.com/post/513.html" title="b端详情页:各种信息聚集地,设计师要如何规划这一亩三分地呢" target="_blank"> <h2 class="f-15">b端详情页:各种信息聚集地,设计师要如何规划这一亩三分地呢</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1123.html" title="漏洞系列一一看我一招征服漏洞 SSRF" target="_blank"> <h2 class="f-15">漏洞系列一一看我一招征服漏洞 SSRF</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1120.html" title="Web前端需要学什么?Web前端开发需要学习哪些?" target="_blank"> <h2 class="f-15">Web前端需要学什么?Web前端开发需要学习哪些?</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1118.html" title="接口测试遇到500报错?别慌,你的头部可能有点问题" target="_blank"> <h2 class="f-15">接口测试遇到500报错?别慌,你的头部可能有点问题</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1689.html" title="前端Flex布局可视化布局工具介绍,vue和html5快速设计利器" target="_blank"> <h2 class="f-15">前端Flex布局可视化布局工具介绍,vue和html5快速设计利器</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1140.html" title="「资讯」为强迫用户使用Edge浏览器,微软又出新招数" target="_blank"> <h2 class="f-15">「资讯」为强迫用户使用Edge浏览器,微软又出新招数</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1682.html" title="HTML 简介(html简介及优缺点)" target="_blank"> <h2 class="f-15">HTML 简介(html简介及优缺点)</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1242.html" title="HBuilderX,uni-app创建HTML5项目,同时支持浏览器和移动端" target="_blank"> <h2 class="f-15">HBuilderX,uni-app创建HTML5项目,同时支持浏览器和移动端</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1499.html" title="现在页面实时聊天都使用Websocket技术实现吗?" target="_blank"> <h2 class="f-15">现在页面实时聊天都使用Websocket技术实现吗?</h2> </a> </li> <li> <a href="http://www.zhezhongyun.com/post/1684.html" title="关于HTML5被简称做H5,你怎么看?(html5缩写)" target="_blank"> <h2 class="f-15">关于HTML5被简称做H5,你怎么看?(html5缩写)</h2> </a> </li> </ul> </dd> </dl> <dl class="function" id="divPrevious"> <dt class="function_t">最近发表</dt><dd class="function_c"> <ul><li><a title="perl基础——循环控制_principle循环" href="http://www.zhezhongyun.com/post/5190.html">perl基础——循环控制_principle循环</a></li> <li><a title="CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅" href="http://www.zhezhongyun.com/post/5189.html">CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅</a></li> <li><a title="CHAPTER 5 The King&#39;S Musketeers and the Cardinal&#39;S Guards 第五章 国王的火枪手和红衣主教的卫士" href="http://www.zhezhongyun.com/post/5188.html">CHAPTER 5 The King&#39;S Musketeers and the Cardinal&#39;S Guards 第五章 国王的火枪手和红衣主教的卫士</a></li> <li><a title="CHAPTER 3 The Audience 第三章 接见" href="http://www.zhezhongyun.com/post/5187.html">CHAPTER 3 The Audience 第三章 接见</a></li> <li><a title="别搞印象流!数据说明谁才是外线防守第一人!" href="http://www.zhezhongyun.com/post/5186.html">别搞印象流!数据说明谁才是外线防守第一人!</a></li> <li><a title="V-Day commemorations prove anti-China claims hollow" href="http://www.zhezhongyun.com/post/5185.html">V-Day commemorations prove anti-China claims hollow</a></li> <li><a title="EasyPoi使用_easypoi api" href="http://www.zhezhongyun.com/post/5184.html">EasyPoi使用_easypoi api</a></li> <li><a title="关于Oracle数据库12c 新特性总结_oracle数据库12514" href="http://www.zhezhongyun.com/post/5183.html">关于Oracle数据库12c 新特性总结_oracle数据库12514</a></li> <li><a title="【开发者成长】JAVA 线上故障排查完整套路!" href="http://www.zhezhongyun.com/post/5182.html">【开发者成长】JAVA 线上故障排查完整套路!</a></li> <li><a title="使用 Python 向多个地址发送电子邮件" href="http://www.zhezhongyun.com/post/5181.html">使用 Python 向多个地址发送电子邮件</a></li> </ul> </dd> </dl> <dl class="function" id="divTags"> <dt class="function_t">标签列表</dt><dd class="function_c"> <ul><li><a title="HTML 教程" href="http://www.zhezhongyun.com/tags-1.html">HTML 教程<span class="tag-count"> (33)</span></a></li> <li><a title="HTML 简介" href="http://www.zhezhongyun.com/tags-3.html">HTML 简介<span class="tag-count"> (35)</span></a></li> <li><a title="HTML 实例/测验" href="http://www.zhezhongyun.com/tags-46.html">HTML 实例/测验<span class="tag-count"> (32)</span></a></li> <li><a title="HTML 测验" href="http://www.zhezhongyun.com/tags-47.html">HTML 测验<span class="tag-count"> (32)</span></a></li> <li><a title="JavaScript 和 HTML DOM 参考手册" href="http://www.zhezhongyun.com/tags-54.html">JavaScript 和 HTML DOM 参考手册<span class="tag-count"> (32)</span></a></li> <li><a title="HTML 拓展阅读" href="http://www.zhezhongyun.com/tags-55.html">HTML 拓展阅读<span class="tag-count"> (30)</span></a></li> <li><a title="HTML文本框样式" href="http://www.zhezhongyun.com/tags-60.html">HTML文本框样式<span class="tag-count"> (31)</span></a></li> <li><a title="HTML滚动条样式" href="http://www.zhezhongyun.com/tags-61.html">HTML滚动条样式<span class="tag-count"> (34)</span></a></li> <li><a title="HTML5 浏览器支持" href="http://www.zhezhongyun.com/tags-113.html">HTML5 浏览器支持<span class="tag-count"> (33)</span></a></li> <li><a title="HTML5 新元素" href="http://www.zhezhongyun.com/tags-114.html">HTML5 新元素<span class="tag-count"> (33)</span></a></li> <li><a title="HTML5 WebSocket" href="http://www.zhezhongyun.com/tags-131.html">HTML5 WebSocket<span class="tag-count"> (30)</span></a></li> <li><a title="HTML5 代码规范" href="http://www.zhezhongyun.com/tags-132.html">HTML5 代码规范<span class="tag-count"> (32)</span></a></li> <li><a title="HTML5 标签" href="http://www.zhezhongyun.com/tags-135.html">HTML5 标签<span class="tag-count"> (717)</span></a></li> <li><a title="HTML5 标签 (已废弃)" href="http://www.zhezhongyun.com/tags-137.html">HTML5 标签 (已废弃)<span class="tag-count"> (75)</span></a></li> <li><a title="HTML5电子书" href="http://www.zhezhongyun.com/tags-141.html">HTML5电子书<span class="tag-count"> (32)</span></a></li> <li><a title="HTML5开发工具" href="http://www.zhezhongyun.com/tags-142.html">HTML5开发工具<span class="tag-count"> (34)</span></a></li> <li><a title="HTML5小游戏源码" href="http://www.zhezhongyun.com/tags-143.html">HTML5小游戏源码<span class="tag-count"> (34)</span></a></li> <li><a title="HTML5模板下载" href="http://www.zhezhongyun.com/tags-144.html">HTML5模板下载<span class="tag-count"> (30)</span></a></li> <li><a title="HTTP 状态消息" href="http://www.zhezhongyun.com/tags-159.html">HTTP 状态消息<span class="tag-count"> (33)</span></a></li> <li><a title="HTTP 方法:GET 对比 POST" href="http://www.zhezhongyun.com/tags-160.html">HTTP 方法:GET 对比 POST<span class="tag-count"> (33)</span></a></li> <li><a title="键盘快捷键" href="http://www.zhezhongyun.com/tags-168.html">键盘快捷键<span class="tag-count"> (35)</span></a></li> <li><a title="标签" href="http://www.zhezhongyun.com/tags-171.html">标签<span class="tag-count"> (226)</span></a></li> <li><a title="HTML button formtarget 属性" href="http://www.zhezhongyun.com/tags-218.html">HTML button formtarget 属性<span class="tag-count"> (30)</span></a></li> <li><a title="opacity 属性" href="http://www.zhezhongyun.com/tags-319.html">opacity 属性<span class="tag-count"> (32)</span></a></li> <li><a title="transition 属性" href="http://www.zhezhongyun.com/tags-360.html">transition 属性<span class="tag-count"> (33)</span></a></li> </ul> </dd> </dl> </div> </div> </div> </div> <div class="footer"> <div class="wide ta-c f-12"> </div> </div> <div class="fixed-box "> <ul> <li class="pchide wapflex"><a href="http://www.zhezhongyun.com/"><i class="fa fa-home"></i> 首页</a></li> <li><a href="http://www.zhezhongyun.com/shoulu.html" title="收录申请" target="_blank"><i class="fa fa-chain-broken mr5"></i>收录</a></li> <li><span class="gotop"><i class="fa fa-caret-up mr5"></i> 顶部</span></li> </ul> </div> <script src="http://www.zhezhongyun.com/zb_users/theme/tx_hao/script/txcstx.min.js?v=2024-12-04"></script> </body> </html><!--134.50 ms , 13 queries , 3663kb memory , 0 error-->