React-redux "connect" explained

Redux is a terribly simple library for state management, and has made working with React more manageable for everyone. 

Redux是一个简单的状态管理库,可以让React状态管理变得更加容易。

However, there are a lot of cases where people blindly follow boilerplate code to integrate redux with their React application without understanding all the moving parts involved.

然而,很多情况下,人们盲目遵循示例代码,将redux与其React应用程序集成在一起,而并不了解引入的所有组件。

· React and redux on their own (React和redux本身是独立的)

At this point it’s hard for some to believe, but redux and React are actually two separate libraries which can and have been used completely independent of each other. Lets take a look at redux’s state management flow :

在这一点上,它很难让人相信,但事实上这两个库的确都能独立使用。让我们花点时间看看redux状态管理流:

reudx状态管理流

If you have worked with redux before, you know that its functionality revolves around a “store”, which is where the state of the application lives. 

如果你以前用过redux,你应该知道它的功能围绕store,也就是应用程序状态保存的地方。

There is no way anyone can directly modify the store. The only way to do so is through reducers, and the only way to trigger reducers is to dispatch actions. So ultimately :

store是不能直接修改的。唯一只能通过reducers修改,而触发reducers要通过发送actions。总而言之:

 - To change data, we need to dispatch an action (要改变数据,我们只能通过发送action)

On the other hand, when we want to retrieve data, we do not get it directly from the store. 

另一方面,当我们想去获取数据时,我们不能直接从store获得。

Instead, we get a snapshot of the data in the store at any point in time using store.getState(), which gives us the “state” of the application as on the time at which we called the getState method.

作为替代,我们能通过store的getState()方法得到store中任一时刻的数据,就像某一时刻定格的照片。当我们调用getState方法时,我们就能获得应用程序那个时刻的状态。总而言之:

 - To obtain data we need to get the current state of the store (要获取数据,我们就要获得store中当前的状态)

Now, let’s come to the (simplified) component structure of a standard react todo-mvc application:

现在,让我们来看看标准的react Todo-mvc应用结构:

标准的react Todo-mvc应用

· Putting them together (把react和redux结合起来)

If we want to link our React application with the redux store, we first have to let our app know that this store exists. This is where we come to the first major part of the react-redux library, which is the Provider.

如果我们想将React应用和redux store连接起来,首先要让我们的应用知道store的存在。这是我们要迈过的第一步,它就是react-redux库中提供的Provider组件。

Provider is a React component given to us by the “react-redux” library. It serves just one purpose : to “provide” the store to its child components.

Provider是一个由react-redux库提供的React组件。它的作用只有一个目的:将store传给它的子组件。

// This is the store we create with redux's createStore

const store = createStore(todoApp,{})

// Provider is given the store as a prop

render( <Provider store={store}>  <App/> </Provider> , document.getElementById('app-node'))

Since the provider only makes the store accessible to it’s children, and we would ideally want our entire app to access the store, the most sensible thing to do would be to put our App component within Provider.

由于provider只让store对于它的孩子可访问,我们的理想是想让整个app访问store,最可行的方法就是将我们的App组件放进Provider中。

If we were to follow the previous diagram, the Provider node would be represented as a parent node on top of the App node. However, because of the utility that Provider gives us, I feel it’s more appropriate to represent it as something which “wraps” the entire application tree, like this :

如果按照我们接着之前的描述,则Provider和App节点被当作父子节点。然而,鉴于Provider组件发挥的作用,我觉得它更恰当的应该被描述为包裹整个应用的存在。就像这样:

Provider包含了整个应用

· Connect 

Now that we have “provided” the redux store to our application, we can now connect our components to it. 

既然我们已经用Provider组件把redux store提供给了我们的应用,那我们现在就把组件和它连接起来。

We established previously that there is no way to directly interact with the store. 

我们之前建立的应用,是不能直接和store交互的。

We can either retrieve data by obtaining its current state, or change its state by dispatching an action (we only have access to the top and bottom component of the redux flow diagram shown previously).

我们只能通过它的当前状态获取数据,或者通过发送一个action去改变它的状态。(我们只能用之前的redux流程图中的顶部和底部的组件)

This is precisely what connect does. Consider this piece of code, which uses connect to map the stores state and dispatch to the props of a component :

这正是connect所做的。考虑这段代码,实用connect去映射stores状态和发送给组件的属性:

connect

mapStateToProps and mapDispatchToProps are both pure functions that are provided the stores “state” and “dispatch” respectively. Furthermore, both functions have to return an object, whose keys will then be passed on as the props of the component they are connected to.

mapStateToProps和mapDispatchToProps均是纯函数,分别为stores提供state和dispatch。此外,这两函数必须返回一个对象,对象的键将被当作它们所连接组件的属性来传递。

In this case, mapStateToProps returns an object with only one key : “todo”, and mapDispatchToProps returns an object with the destroyTodo key.

在这种情况下,mapStateToProps 返回一个只有“todo”键的对象,mapDispatchToProps 返回一个带有destroyTodo键的对象。

The connected component (which is exported) provides todo and destroyTodo as props to TodoItem.

所被连接的组件(那个被输出的“TodoItem”)把 todo 和 destroyTodo 作为属性提供给TodoItem。


connect within provider

It’s important to note that only components within the Provider can be connected (In the above diagram, the connect is done through the Provider).

请注意只有Provider中的组件能够被connected。

Redux is a powerful tool and even more so when combined with React. It really helps to know why each part of the react-redux library is used, and hopefully after reading this post, the function of Provider and connect is clear.

Redux是一个强大的工具,甚至与React结合也是如此。 这真的有助于了解react-redux库每个部分的使用,并且希望在阅读这篇文章后,你对Provider和connect的认识更清晰。

原文:http://www.sohamkamani.com/blog/2017/03/31/react-redux-connect-explained/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,363评论 0 23
  • 我发现梦想是回事,现实又是回事。第一天瑜伽,也是够拼,两个膝盖都是淤青。原本以为喜欢就能做到,可事实是喜欢是回...
    逃避现实阅读 139评论 0 0
  • 总结力 每次听Grace的课,即使是相同的主题,也总是会有不同的收获。这次的总结力给我的收获和启发包括以下3点 职...
    兰子2016阅读 422评论 1 1
  • 如果我没有记错,再有一个多小时你就要到家了,期间你没有发来任何信息,我也不敢主动找你,那个总喜欢对你死缠烂打的我已...
    半忆夏阅读 248评论 0 0
  • 每日心语:爱你多深,伤口就有多深。
    遇见M逗阅读 146评论 0 0