簡體   English   中英

如何使用 React.js 在 map 方法中重構代碼冗余

[英]How to refactor a code redundancy in the map method using React.js

以下是代碼,使用 React.js,我在其中調用 swapi(StarWars API)的 arrays 每個 10 個字符及其每頁內部的描述

結果0 - 結果4

const CardList = ({ results0, results1, results2, results3, results4 }) => {
  return(
    <Fragment>
    { 

以下是重復代碼的開始。 results1...results4 使用相同的.map方法並返回相同的卡信息。

 results0.map((people, i) => {
      return (
        <Card
          key={i}
          name={results0[i].name}
          skin_color={results0[i].skin_color}
          eye_color={results0[i].eye_color}
          birth_year={results0[i].birth_year}
          gender={results0[i].gender}
        />
      );
    })

result1.map(same values)=>{same function} ...result4.map(same values)=>{same function},

  
     }
     </Fragment>      
     );  
  }; 
export default CardList;

有沒有辦法縮短代碼?

首先,如果回調相同,將它們分解到實用程序 function 中,記住使用數組的當前值::map 回調

const renderPerson = (person, i) => (
  <Card
    key={i}
    name={person.name}
    skin_color={person.skin_color}
    eye_color={person.eye_color}
    birth_year={person.birth_year}
    gender={person.gender}
  />
);

這可以使用 object 解構進一步減少

const renderPerson = ({ birth_year, eye_color, gender, name, skin_color }, i) => (
  <Card
    key={i}
    name={name}
    skin_color={skin_color}
    eye_color={eye_color}
    birth_year={birth_year}
    gender={gender}
  />
);

現在您已將代碼簡化為更易於管理

results0.map(renderPerson)
results1.map(renderPerson)
results2.map(renderPerson)
results3.map(renderPerson)
results4.map(renderPerson)

但是你還有一些重復,你可以將 5 個結果道具轉儲到一個數組中,也可以將它們 map

[results0, results1, results2, results3, results4].map((result, i) => (
  <Fragment key={i}>
    {result.map(renderPerson)}
  </Fragment>
)) 

您可以像這樣將所有 arrays 組合起來,然后一步完成 map

const CardList = ({ results0, results1, results2, results3, results4 }) => {
    return (
        <Fragment>
            {[...results0, ...results1, ...results2, ...results3, ...results4].map((result, i) =>
                (<Card
                    key={i}
                    name={result.name}
                    skin_color={result.skin_color}
                    eye_color={result.eye_color}
                    birth_year={result.birth_year}
                    gender={rresult.gender}
                />))
            }
        </Fragment>
    );
};
export default CardList;

在中使用.map<div> 標簽-react.js</div><div id="text_translate"><p> 我真的很感激任何幫助。 所以該項目是一個反應項目。</p><p> 這是我獲取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的內容:獲取請求</a></p><p>這是我從服務器響應的內容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我試了一下 Postman,看看我收到的是什么類型的 json。 這是它返回的內容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 測試</a></p><p>我遇到的問題是我無法 map 收到的數據。 一旦輸入鏈接,我只想在首頁上以一種很好的方式顯示接收到的數據。</p><p> 是否可以將 map 放在 div 標簽內? 或者還有另一種方法嗎?</p><p> <em><strong>請注意該項目是作為功能組件編寫的</strong></em></p></div>

[英]Using .map in <div> tag - react.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 使用 map 方法,如何在 React.js 中使用多個類名? 重構React.js代碼以提高樣式和效率 在 react.js 中使用 map 方法時道具不起作用 如何使用 map 方法分離每個 object output - React.js 如何將CARTO映射集成到react.js代碼中 如何重構 <List> 在React JSX中使用map()方法添加內容? 我如何在React.js中重構這些變量 在 react.js 中使用 Array.map 使用 react.js 從-到映射 在中使用.map<div> 標簽-react.js</div><div id="text_translate"><p> 我真的很感激任何幫助。 所以該項目是一個反應項目。</p><p> 這是我獲取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的內容:獲取請求</a></p><p>這是我從服務器響應的內容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我試了一下 Postman,看看我收到的是什么類型的 json。 這是它返回的內容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 測試</a></p><p>我遇到的問題是我無法 map 收到的數據。 一旦輸入鏈接,我只想在首頁上以一種很好的方式顯示接收到的數據。</p><p> 是否可以將 map 放在 div 標簽內? 或者還有另一種方法嗎?</p><p> <em><strong>請注意該項目是作為功能組件編寫的</strong></em></p></div>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM