|
Post by account_disabled on Jan 28, 2024 5:03:25 GMT
本文是 Microsoft Web 开发系列的一部分。感谢您对合作伙伴的支持,使 SitePoint 成为可能。 Backward Skip 10s Play Video Forward Skip 10s 管理数据对于任何应用程序都是至关重要的。通过应用程序的用户界面 (UI) 编排数据流可能具有挑战性。通常,当今 React 在其组件中使用的数的 Web 应用程序具有复杂的 UI,因此修改 UI 一个区域中的数据可能会直接或间接影响 UI 的其他区域。通过 Knockout.js 和 Angular.js 进行双向数据绑定是解决此问题的常用方法。 对于某些应用程序(特别是具有简单数据流的应用程序),双向绑定可能是一个足够且快速的解决方案。 对于更复杂的应用程序,双向数据绑定可能是不够的,并且会妨碍有效的 UI 设计。React 并没有解决应用程序数据流这个更大的问题(尽管Flux 做到了),但它确实解决了单个组件内的数据流问题。 在单个组件的上下文中,React 既解决了数据流问题,又解决了 WhatsApp 号码数据 更新 UI 以反映数据流结果的问题。UI 更新的第二个问题是使用名为 Reconciliation 的模式解决的,该模式涉及虚拟 DOM 等创新理念。下一篇文章将详细研究调节。本文重点讨论数据流的第一个问题,以及 React 在其组件中使用的数据类型。 组件数据的种类 Learn to Code with JavaScript React 组件中的数据存储为属性或状态。 属性是组件的输入值。它们在渲染组件和初始化状态时使用(稍后讨论)。实例化组件后,属性应被视为不可变。属性值只能在实例化组件时设置,然后当组件在 DOM 中重新渲染时,React 会比较新旧属性值,以确定需要更新哪些 DOM。 以下演示了如何设置属性值并根据更新的属性值更新 DOM。 状态数据可以由组件更改,并且通常连接到组件的事件处理程序中。通常,更新状态会触发 React 组件重新渲染自身。在初始化组件之前,必须初始化其状态。初始化值可以包括常量值以及属性值(如上所述)。 与 Angular.js 等框架相比,属性可以被视为单向绑定数据,状态可以被视为双向绑定数据。
|
|