B站 React教程笔记day2(1)Flux入门

https://www.codercto.com/a/25013.html

https://github.com/facebook/flux/tree/master/examples/flux-concepts

简介

  • 基本的 MVC 数据流

  • 复杂的MVC数据流

  • 基本的Flux数据流

  • 复杂的Flux数据流

 

相比MVC模式,Flux多出了更多的箭头跟图标,但是有个关键性的差别是:所有的剪头都指向一个方向,在整个系统中形成一个闭环。

Overview

Flux is a pattern for managing data flow in your application.

译:Flux是app中管理数据流动的一种模式。

The most important concept is that data flows in one direction.

译:最重要的概念就是数据的单项流动

Flux Parts

  • Dispatcher - 调度

  • Store - 仓库

  • Action - 动作

  • View - 界面

Dispatcher

The dispatcher receives actions and dispatches them to stores that have registered with the dispatcher. 

译:dispatcher 接收 actions ,并且把这些 actions 调度 到注册在dispatcher身上的 stores

Every store will receive every action. 

译:每一个 store 接收每一个 action。

There should be only one singleton dispatcher in each application.

译:在 app 中,应该只有一个 dispatcher 实例。

Store

A store is what holds the data of an application.

译:store 是 App 中的数据容器

Stores will register with the application's dispatcher so that they can receive actions. 

译:stores 注册在 app 的 dispatcher 中,目的是让 stores 接收 dispatcher 的 actions

The data in a store must only be mutated by responding to an action. 

译:store 中的数据只有相应 action 时才会被 更改

There should not be any public setters on a store, only getters.

译:stores 不能有公共的 setter 方法,只有 getter 方法

Stores decide what actions they want to respond to. 

译:store 们决定了它们愿意响应哪些 actions

Every time a store's data changes it must emit a "change" event. 

译:每一次 store 的数据变化都触发一个 “change" 事件

There should be many stores in each application.

译:一个 App 可以有许多 store

Actions

Actions define the internal API of your application.

译:Actions 定义了 App 内部中的 API

They capture the ways in which anything might interact with your application.

译:它们捕获任何可能与您的应用程序交互的方式。

They are simple objects that have a "type" field and some data.

译:它们是具有“类型”字段和一些数据的简单对象。

  {
   type: 'delete-todo',
   todoID: '1234',
}

Views

Data from stores is displayed in views.

译: stores 中的数据展示在 views上

Views can use whatever framework you want (In most examples here we will use React). 

译: 视图可以使用您想要的任何框架(在大多数示例中,我们将使用React)。

When a view uses data from a store it must also subscribe to change events from that store. 

译: 当 view 使用来自 store 的数据时,它还必须订阅来自该 store 的更改事件。

Then when the store emits a change the view can get the new data and re-render.

译: 当 store 发射了 change 事件,此时 view 就能得到新的数据并且重新渲染。

If a component ever uses a store and does not subscribe to it then there is likely a subtle bug waiting to be found.

译: 如果一个 component 使用 store 中的数据,却不订阅它,可能会导致一个微妙的错误。

Actions are typically dispatched from views as the user interacts with parts of the application's interface.

译: 在 user 与 应用程序界面的一部分 交互时,actions 被 views 所调度

猜你喜欢

转载自www.cnblogs.com/houfee/p/10873592.html