React 19 中的新增功能
本篇文章给大家分享《React 19 中的新增功能》,覆盖了文章的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
行动
react 应用程序中的一个常见用例是执行数据突变,然后更新状态作为响应。例如,当用户提交表单以更改其姓名时,您将发出 api 请求,然后处理响应。过去,您需要手动处理挂起状态、错误、乐观更新和顺序请求。
例如,您可以在 usestate 中处理挂起和错误状态:
// before actions function updatename({}) { const [name, setname] = usestate(""); const [error, seterror] = usestate(null); const [ispending, setispending] = usestate(false); const handlesubmit = async () => { setispending(true); const error = await updatename(name); setispending(false); if (error) { seterror(error); return; } redirect("/path"); }; return (setname(event.target.value)} /> {error &&); }{error}
}
在 react 19 中,我们添加了对在转换中使用异步函数的支持,以自动处理挂起状态、错误、表单和乐观更新。
例如,您可以使用 usetransition 为您处理挂起状态:
// using pending state from actions function updatename({}) { const [name, setname] = usestate(""); const [error, seterror] = usestate(null); const [ispending, starttransition] = usetransition(); const handlesubmit = () => { starttransition(async () => { const error = await updatename(name); if (error) { seterror(error); return; } redirect("/path"); }) }; return (setname(event.target.value)} /> {error &&); }{error}
}
异步转换将立即将 ispending 状态设置为 true,发出异步请求,并在任何转换后将 ispending 切换为 false。这使您可以在数据更改时保持当前 ui 的响应能力和交互性。
笔记
按照惯例,使用异步转换的函数称为“操作”。
操作会自动为您管理提交数据:
挂起状态:操作提供一个挂起状态,该状态在请求开始时启动,并在提交最终状态更新时自动重置。
乐观更新:操作支持新的 useoptimistic 挂钩,因此您可以在提交请求时向用户显示即时反馈。
错误处理:操作提供错误处理,以便您可以在请求失败时显示错误边界,并自动将乐观更新恢复为其原始值。
表单:
在下一节中,我们将详细介绍 react 19 中的每个新 action 功能。
新钩子:useactionstate
为了使操作的常见情况更容易,我们添加了一个名为 useactionstate 的新钩子:
const [error, submitaction, ispending] = useactionstate( async (previousstate, newname) => { const error = await updatename(newname); if (error) { // you can return any result of the action. // here, we return only the error. return error; } // handle success return null; }, null, );
useactionstate 接受一个函数(“action”),并返回一个包装的 action 来调用。这是有效的,因为动作是组合的。当调用被包装的 action 时,useactionstate 会返回该 action 的最后结果作为数据,并将该 action 的挂起状态作为挂起。
笔记
react.useactionstate 以前在 canary 版本中称为 reactdom.useformstate,但我们已重命名它并弃用了 useformstate。
请参阅#28491 了解更多信息。
有关更多信息,请参阅 useactionstate 的文档。
react dom:
笔记
use 不支持渲染中创建的 promise。
如果你尝试传递在渲染中创建的 promise 来使用,react 会发出警告:
error a component was suspended by an uncached promise. creating promises inside a client component or hook is not yet supported, except via a suspense-compatible library or framework.
要修复此问题,您需要从支持缓存 promise 的 suspense 支持的库或框架传递 promise。将来,我们计划发布一些功能,以便更轻松地在渲染中缓存 promise。
您还可以使用 use 来读取上下文,允许您有条件地读取上下文,例如在提前返回后:
import {use} from 'react'; import themecontext from './themecontext' function heading({children}) { if (children == null) { return null; } // this would not work with usecontext // because of the early return. const theme = use(themecontext); return ({children}
); }
use api只能在render中调用,类似于hooks。与钩子不同,use 可以有条件地调用。未来我们计划支持更多在渲染和使用中消耗资源的方式。
更多信息请参阅使用文档。
新的 react dom 静态 api
我们在react-dom/static中添加了两个新的api来生成静态站点:
- 预渲染
- prerendertonodestream
这些新 api 通过等待数据加载以生成静态 html 来改进 rendertostring。它们旨在与 node.js streams 和 web streams 等流环境配合使用。例如,在 web stream 环境中,您可以使用 prerender 将 react 树预渲染为静态 html:
import { prerender } from 'react-dom/static'; async function handler(request) { const {prelude} = await prerender(, { bootstrapScripts: ['/main.js'] }); return new Response(prelude, { headers: { 'content-type': 'text/html' }, }); }
预渲染 api 将等待所有数据加载,然后返回静态 html 流。流可以转换为字符串,或与流响应一起发送。他们不支持加载流媒体内容,这是
反应服务器组件
服务器组件
服务器组件是一个新选项,允许在捆绑之前在与客户端应用程序或 ssr 服务器分开的环境中提前渲染组件。这个单独的环境是 react server components 中的“服务器”。服务器组件可以在构建时在 ci 服务器上运行一次,也可以使用 web 服务器针对每个请求运行。
react 19 包含 canary 通道中包含的所有 react server 组件功能。这意味着随服务器组件一起提供的库现在可以将 react 19 作为对等依赖项,并具有反应服务器导出条件,以便在支持全栈 react 架构的框架中使用。
理论要掌握,实操不能落!以上关于《React 19 中的新增功能》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
-
- 文章 · 前端 | 7小时前 |
- JavaScript数组元素总和的计算技巧
- 143浏览 收藏
-
- 文章 · 前端 | 7小时前 | window.location window.open window.location.replace window.location.assign
- JavaScript页面跳转技巧与代码实现
- 184浏览 收藏
-
- 文章 · 前端 | 7小时前 |
- JavaScript数组求和的实用技巧
- 407浏览 收藏
-
- 文章 · 前端 | 8小时前 |
- JavaScript中Array.prototype.reduceRight使用及示例
- 273浏览 收藏
-
- 文章 · 前端 | 8小时前 |
- JavaScript音频可视化实现的独门技巧
- 334浏览 收藏
-
- 文章 · 前端 | 8小时前 |
- JavaScript中的闭包详解及应用
- 353浏览 收藏
-
- 文章 · 前端 | 8小时前 | 性能优化 事件冒泡 事件委托 addEventListener 移除监听器
- JavaScript按钮点击事件监听的简易技巧
- 311浏览 收藏
-
- 文章 · 前端 | 8小时前 |
- JavaScriptWebWorkers使用指南及示例代码
- 315浏览 收藏
-
- 文章 · 前端 | 8小时前 |
- Vue.js中CompositionAPI与OptionsAPI对比使用
- 151浏览 收藏
-
- 文章 · 前端 | 8小时前 |
- call与apply在JavaScript中的区别详解
- 238浏览 收藏
-
- 文章 · 前端 | 9小时前 |
- JavaScript中Array.prototype.reduceRight使用及示例
- 420浏览 收藏
-
- 文章 · 前端 | 9小时前 |
- Vue.jsSSR实现攻略:服务端渲染详解
- 472浏览 收藏