const[error, submitAction, isPending]=useActionState( async(previousState, newName)=>{ const error =awaitupdateName(newName); if(error){ // You can return any result of the action. // Here, we return only the error. return error; }
用法:当组件卸载时,React 会调用从 ref 回调返回的清理函数。这适用于 DOM refs、对类组件的 refs 和 useImperativeHandle。
<input ref={(ref)=>{ // ref created
// NEW: return a cleanup function to reset // the ref when element is removed from DOM. return()=>{ // ref cleanup }; }} />
useDeferredValue 初始值
功能:为 useDeferredValue 添加了 initialValue 选项。
用法:当提供 initialValue 时,useDeferredValue 将在组件的初始渲染中将其作为 value 返回,并在后台使用返回的 deferredValue 安排重新渲染。
functionSearch({deferredValue}){ // On initial render the value is ''. // Then a re-render is scheduled with the deferredValue. const value =useDeferredValue(deferredValue,'');
functionComponentTwo(){ return( <div> <p>{...}</p> <link rel="stylesheet" href="baz" precedence="default"/><-- will be inserted between foo & bar </div> ) }
functionApp(){ <html> <body> <MyComponent> ... <MyComponent>// won't lead to duplicate script in the DOM </body> </html> }
支持预加载资源
功能:包含了一组新的 API,用于加载和预加载浏览器资源。
prefetchDNS: 当你可能实际上没有从该主机请求任何内容时。
preconnect: 当你要请求某些内容但不确定是什麽时。
preload: 预加载资源,例如字体、样式表等。
preinit: 渴望加载并执行脚本。
import{ prefetchDNS, preconnect, preload, preinit }from'react-dom' functionMyComponent(){ preinit('https://.../path/to/some/script.js',{as:'script'})// loads and executes this script eagerly preload('https://.../path/to/font.woff',{as:'font'})// preloads this font preload('https://.../path/to/stylesheet.css',{as:'style'})// preloads this stylesheet prefetchDNS('https://...')// when you may not actually request anything from this host preconnect('https://...')// when you will request something but aren't sure what }