首頁 web前端 js教程 如何在 React 渲染函數中正確使用 Async/Await?

如何在 React 渲染函數中正確使用 Async/Await?

Oct 18, 2024 pm 03:28 PM

How to Use Async/Await Correctly in React Render Functions?

Understanding Async/Await in React Render Functions

Asynchronous programming, enabled by async/await syntax, is common in back-end development, such as Node.js, but can also be applied in front-end scenarios within React render functions.

Use Case: Geocoding with react-geocode

Consider a scenario where you need to obtain the place name for multiple locations using the react-geocode library and display them in a React table:

<code class="js">import React, { useEffect, useState } from 'react';
import Geocode from 'react-geocode';
import _ from 'lodash';

const GeocodeTable = ({ locations }) =&amp;gt; {
  const [addresses, setAddresses] = useState([]);

  useEffect(() =&amp;gt; {
    Promise.all(locations.map(async (loc) =&amp;gt; {
      const address = await Geocode.fromLatLng(loc[0], loc[1]);
      return address.results[0].formatted_address;
    }))
    .then(results =&amp;gt; setAddresses(results));
  }, [locations]);

  return (
    &amp;lt;tbody&amp;gt;
      {addresses.map((addr, idx) =&amp;gt; (
        &amp;lt;tr key={idx}&amp;gt;
          &amp;lt;td&amp;gt;{addr}&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;Goa&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;asdsad&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;{_.get(loc, 'driverId.email', '')}&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;{_.get(loc, 'driverId.mobile', '')}&amp;lt;/td&amp;gt;
        &amp;lt;/tr&amp;gt;
      ))}
    &amp;lt;/tbody&amp;gt;
  );
};</code>
登入後複製

Mistakes to Avoid

In your original code, you attempted to use async/await directly within the map function of the render function. This is not supported and will result in an empty return.

Best Practices

  • Separate Data Fetching from Displaying: The recommended approach is to separate data fetching from rendering. Use a parent component to perform asynchronous operations (like geocoding) and conditionally render a child component when the data is available.
  • Use Memoization: To optimize performance, consider using memoization techniques, such as useMemo in the parent component, to avoid redundant data fetching.
  • TypeScript Support: If using TypeScript, consider defining types for the geocoding results to improve type safety and avoid potential errors.

以上是如何在 React 渲染函數中正確使用 Async/Await?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在JavaScript中替換字符串字符 在JavaScript中替換字符串字符 Mar 11, 2025 am 12:07 AM

在JavaScript中替換字符串字符

自定義Google搜索API設置教程 自定義Google搜索API設置教程 Mar 04, 2025 am 01:06 AM

自定義Google搜索API設置教程

示例顏色json文件 示例顏色json文件 Mar 03, 2025 am 12:35 AM

示例顏色json文件

8令人驚嘆的jQuery頁面佈局插件 8令人驚嘆的jQuery頁面佈局插件 Mar 06, 2025 am 12:48 AM

8令人驚嘆的jQuery頁面佈局插件

10個jQuery語法熒光筆 10個jQuery語法熒光筆 Mar 02, 2025 am 12:32 AM

10個jQuery語法熒光筆

構建您自己的Ajax Web應用程序 構建您自己的Ajax Web應用程序 Mar 09, 2025 am 12:11 AM

構建您自己的Ajax Web應用程序

什麼是這個&#x27;在JavaScript? 什麼是這個&#x27;在JavaScript? Mar 04, 2025 am 01:15 AM

什麼是這個&#x27;在JavaScript?

10 JavaScript和JQuery MVC教程 10 JavaScript和JQuery MVC教程 Mar 02, 2025 am 01:16 AM

10 JavaScript和JQuery MVC教程

See all articles