本週,我們將討論新的 React 19 更新和掛鉤。在經歷並使用了其中一些新更新後,我只能同意它簡化了開發人員在建立應用程式(尤其是與互動式表單相關的應用程式)時所經歷的一些嚴格活動。
加入我,讓我們來探索其中的一些新更新。
React 編譯器:React 編譯器選擇您的 React 程式碼,將其轉換為瀏覽器的 JavaScript 程式碼,並管理元件或使用者介面的狀態。這個獨特的操作有助於優化應用程式的效能。
如果您熟悉useMemo 鉤子、useCallback 鉤子和React.Memo,您就會明白它們有助於記憶(存儲值或函數以供將來使用)您的組件,因此它們不必重新編寫當沒有狀態更改時渲染。當狀態改變時,React 會重新渲染相關元件及其子元件,而當程式碼沒有變化時,元件將保持原樣,保留元件或鉤子中的先前記憶體、值和狀態以供將來使用用途;從而最佳化元件的性能。這正是 React 編譯器自動執行的操作,無需手動呼叫任何上述鉤子。
表單操作:使用React的最大優點之一是狀態的管理和突變,這主要發生在我們使用時
,這主要發生在我們使用時 元素。表單幫助我們更有效地建立和處理使用者互動。 使用表單操作,您不需要像onSubmit和onChange這樣的事件處理程序來實現資料的即時突變,而是我們可以傳遞
actionconst myFunction = async (formData) => { const [name, setName] = useState("") const updatedName = await nameChange(formData.get("name")) setName(updatedName) } <form action={myFunction}> <input type="name" name="name" _// this is important to be able to get our data //_ /> <button type="submit">Submit</button> </form>
元素。 透過這種方法,我們不需要應用useState 透過event.target.value
來改變數據,而是在myFunction 我們可以透過參數取得變異數據。 這表示我們必須從表單中的 元素設定一個 name 屬性。使用此方法意味著我們允許 React 透過
Native React form Object 處理表單本身,而不是透過事件處理程序手動變更狀態。
:React 19 允許在捆綁之前在伺服器上渲染元件,在與客戶端應用程式或 SSR 伺服器設定不同的環境中。 Server 元件與 SSR(伺服器端渲染)不同,而是 React Server Components 中的一個獨立的伺服器環境。
此功能允許元件提前預渲染,從而實現快速內容顯示並縮短載入時間。
元資料:React 19 允許靈活的元資料。開發者可以透過 DocumentHead 元件管理各個元件的 title、description
以及其他
Const AboutUs = () => { return ( <> <title>Learn more about our company</title> <meta name="AboutUs" description="You can find us on ..." /> // content </> ); }
React 19
有一系列新 鉤子,有些是新的,有些是對現有鉤子的改進。下面我們就來討論一下。
use() 鉤子
use hook 非常通用,也可以用來取代 useEffect,因為它可以用於非同步資料取得。這有助於
實現更整潔、簡潔的程式碼區塊。
警告
:
import {use} from "react" const fetchData = async () => { const response = await fetch("https://........"); return response.json() } const userData = () => { const user = use(fetchData()); return ( <div className='username'>{user.name}</div> ); }
useActionState():這是一個新的鉤子,有助於管理狀態變更。它有助於管理掛起狀態、錯誤處理和最終
狀態更新。
import { useActionState } from "react"; async function incrementFunction(initialState, formData) { return initialState + 1; } function StatefulForm({}) { const [state, formAction, isPending] = useActionState(incrementFunction, 0); console.log(state); return ( <form> <button formAction={formAction}>{state}</button> </form> ) }
From this example, the incrementFunction adds 1 to the state every time the button is clicked. The initialState being 0, and then increases to 1 at the first click of the button, thereby changing the state to 1, and for every other click on the button adds 1 to the last state of the component.
useOptimistic() hook:
This is a new hook that allows users to see the outcome of their action even before the pages load completely. The pages are optimistically rendered to the user even when the server is still in its data fetching mode.
With the hope that data fetching will be successful, React displays the intended result to the client, and when data fetching fails, React reverts to the value of the previous state in order not to display incorrect data. This singular action helps in a seamless and fast display of data thereby improving user experience.
useFormStatus():
As the name implies, useFormStatus gives us the status of our form or form fields. This hook does not take in any parameter, but it sure returns 4 objects:
pending: This returns a boolean value: true or false. It returns true when the form is submitting, and false when the form is submitted.
data: When the form is successfully submitted, we can get the information of the user or object from the form field like this:
(formData.get("name")) (formData.get("address"))
method: The default method of a form is GET, unless stated otherwise. We can get the method of our form with .method. When we are submitting a form, a string method property should be specified as a POST.
action: This is a reference to the function that will be passed to the action prop in our element.
The useFormStatus must always be called from a element or a component that is rendered inside a .
There's quite a few more updates that I can't really write about, so you don't get bored having to read so much. You can click on the React Docs Website to check out some of the other updates.
I hope you had a great time learning with me.
Do well to follow me if you enjoyed my articles.
Thanks, and have a great week ahead my friends.
以上是反應新的更新的詳細內容。更多資訊請關注PHP中文網其他相關文章!