React 中的 Each 和 Of

本文介绍了React中不同的列表渲染方法,如Map、For循环,重点讲解了Each和Of模式,强调其在代码简洁性和可扩展性方面的优点,包括自动key处理和提供过滤、排序等功能。作者还展示了如何实现一个通用的Each组件以支持这些特性。
摘要由CSDN通过智能技术生成

React 中的 Each 和 Of

React 是当今最流行的前端库之一,每个人都有自己的工作方式。有些人更喜欢使用 Redux,有些人更喜欢使用 Context,还有些人更喜欢只使用常规状态工作。这在很大程度上取决于我们正在开发的应用程序以及该应用程序的复杂性。渲染树也是如此,许多人使用 Render props 模式,有些人认为 React 中的面向对象编程更好。

Each 和 Of 模式

同样,渲染也有所不同。Javascript 中的数组有很多方法和技术,您可以在为列表或映射类对象渲染树时使用它们,其中一些是:

使用 Map


{
  customers.map((customer) => {
    return <CustomerCard key={customer.id} customer={customer} />
  }) 
}

使用 For 循环和变体


// 渲染之前

const renderCustomers = () => {
  const cards = []
  for(let i = 0; i < customers.length; i++){
    cards.push(<CustomerCard customer={customers[i]}>)
  }
  return cards; 
}

// 在渲染中

{
  renderCustomers()
}

它们都有自己的优点和缺点。这完全取决于我们想要通过这个实现的输出是什么。还有一种在所有列表项上列出的方法,可以从中获得更多内容


// 在渲染中

<Each of={customers} render={(customer) => {
  return <CustomerCard customer={customer} />
}} />

问题 — 这种方法的好处是什么?

好吧,没有什么好处是我们不能通过其他方法实现的,长话短说。这在你的朋友和同事面前看起来不错。但是让我列出一些名字,这样我们就可以为你的计划增加更多内容:

- 不需要担心呈现卡片的 key 属性

- 传递额外的列表特定功能,如排序或筛选回调

- 提供对所有列表通用的搜索功能

你可以比这更多,但正如我所说的,没有任何好处是你不能用普通方法创造的。

实现

现在,如何实现这一点,就像它看起来的那样。我们需要创建一个名为 Each 的组件,该组件需要是通用的,以便任何组件和列表都可以访问它,而不必担心它。


import { Children } from 'react';

export const Each = ({ render, of }) => {
  return Children.toArray(
    of.map((item, index) => {
      return render(item, index)
    })
  )
}

现在,我们的组件已经准备好采取行动了。由于它只是一个 React 组件,这个高阶函数将支持 React 为组件提供的所有状态、钩子和其他功能。

您还可以通过提供执行数据操作的选项(如下所示)使其更高级:


import { Children } from 'react';  

export const Each = ({ render, of, options }) => {
  if(options.filter){
    return Children.toArray(
      of.filter(options.filter)
      .map((item, index) => {
        return render(item, index)  
      })
    )
  }
  return Children.toArray(  
    of.map((item, index) => {
      return render(item, index)
    })
  )
}

// 用法

<Each of={customers} 
  options = {  
    {
      filter: (customer) => !customer.isBlocked  
    }
  }
  render={(customer) => {
    return <CustomerCard customer={customer} />
  }}  
/>

Vue, React, and Angular are all popular JavaScript frameworks used for building web applications. Vue is a relatively lightweight framework that is known for its simplicity and ease of use. It is often favored by developers who are new to JavaScript frameworks or who prefer a more minimalist approach to development. Vue also has a strong focus on performance and can be used for building both small and large-scale applications. React is developed and maintained by Facebook and is widely used for building complex user interfaces. It uses a virtual DOM and a one-way data flow model which enables it to efficiently manage state and handle large amounts of data. React can be used with other libraries and frameworks, making it highly flexible and customizable. Angular is a full-featured framework developed by Google. It is known for its powerful features and its ability to handle large and complex applications. Angular uses a two-way data binding approach and has a steep learning curve compared to Vue and React. However, it offers a wide range of features, including dependency injection, routing, and animations, making it a popular choice for enterprise-level applications. Ultimately, the choice between Vue, React, and Angular will depend on the specific needs and preferences of the developer or development team. Each framework has its own strengths and weaknesses, and the decision should be based on the project requirements, available resources, and the experience level of the developers involved.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天也想MK代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值