|
Post by account_disabled on Jan 29, 2024 4:16:40 GMT
我猜现在大多数开发人员都在使用某种框架来开发应用程序。框架可以帮助我们构建复杂的应用程序并节省我们的时间。每天,我们都能看到很多关于哪个框架最好、应该先学习哪个框架等等的讨论。所以今天,我想分享一下我的经验,以及为什么我从 React 转向 Cycle.js。 React 可能是当今最流行的前端框架,它拥有一个很棒的社区。我是它的忠实粉丝,它确实帮助我改变了我对网络应用程序以及开发它们的方式的看法。有些开发者喜欢它,有些则认为它没有大家说的那么好。 大多数人开始使用 React 时并没有想到可能有更好的方法来构建 Web 应用程序。这种反思让我尝试了 Cycle.js,这是一个日益流行的新型反应式框架。在本文中,我想解释什么是响应式编程、Cycle.js 的工作原理以及为什么我认为它比 React 更好。那么让我们开始吧! 什么是反应式编程? 反应式编程(RP)是使用异步数据流进行编程。 如果您已经构建了一个网络应用程序,您可能进行了大量的响应式编程。例如,点击事件是异步数据流。我们可以观察它们并执行一些副作用。RP 背后的想法是让我们能够从任何东西创建数据流并对其进行操作。然后,我们对所 WhatsApp 号码数据 有副作用都有相同的抽象,这更容易使用、维护和测试。 您可能在想“为什么我需要这种新的反应式编程?” 答案很简单:反应式编程将帮助您统一代码并使其更加一致。您不需要考虑事情应该如何运作以及如何正确实施它们。无论您处理什么数据(点击事件、HTTP 调用、Web 套接字...),只需以相同的方式编写代码即可。一切都是数据流,每个流都有许多可用于处理它的函数,例如map、 和filter。这些函数将返回可以使用的新流,等等。 响应式编程为您提供了更大的代码抽象。它将让您能够创建交互式用户体验并专注于业务逻辑。 反应性点击 图片取自中的响应式编程 在 JavaScript 中,我们有几个很棒的库来处理数据流。最知名的是RxJS。它是ReactiveX的扩展,ReactiveX 是一个用于使用可观察流进行异步编程的 API。 您可以创建一个 Observable(数据流),并使用各种函数对其进行操作。 第二个是Most.js。它具有最好的性能,他们可以用一些数字来证明这一点:性能比较。 我还想提一下一个小而快速的库,它是由 Cycle.js 的创建者专门为其制作的。它称为xstream。它只有 26 个方法,大约 30kb,是 JS 中响应式编程最快的库之一。 在下面的示例中,被设计成一个小型框架,我想将最小的反应式库附加到它上面。 什么是 Cycle.js? Cycle.js 是一个函数式和反应式 JavaScript 框架。它将您的应用程序抽象为纯函数,main(). 在函数式编程中,函数应该只有输入和输出,没有任何副作用。在 Cycle.js 的main()函数中,输入是来自外部世界的读取效果(源),输出(接收器)是对外部世界的写入效果。管理副作用是使用驱动程序完成的。驱动程序是处理 DOM 效果、HTTP 效果和 Web 套接字等的插件。 Cycle.js 图片取自 Cycle.js 网站 Cycle.js 可以帮助我们构建用户界面、测试它们并编写可重用的代码。每个组件只是一个可以独立运行的纯函数。
|
|