简体   繁体   English

侦听器()在Redux商店中做了什么?

[英]What listener() does in the Redux store?

I'm watching Dan Abramov's Redux videos on Egghead. 我在Egghead上观看Dan Abramov的Redux视频。 In the video where he implements the Redux Store from scratch, he includes this code (around 1:28 - https://egghead.io/lessons/javascript-redux-implementing-store-from-scratch ): 在他从零开始实现Redux Store的视频中,他包含了这段代码(大约1:28 - https://egghead.io/lessons/javascript-redux-implementing-store-from-scratch ):

const dispatch = (action) => {
  state = reducer(state, action);
  listeners.forEach(listener => listener());
};

So this code iterates through each item in the listener array, and I understand that each listener needs to be updated, but I don't get what listener() is doing. 所以这段代码遍历了监听器数组中的每个项目,我理解每个监听器都需要更新,但我没有得到listener()正在做的事情。 Where does this function come from? 这个功能来自哪里?

createStore method have listeners variable. createStore方法有listeners变量。 It is a array of functions which should be called when store updates. 它是一组函数,应在存储更新时调用。 You can add own function into listeners through method subscribe method of store . 您可以通过store方法subscribe方法将自己的函数添加到listeners中。

For example: 例如:

const store = createStore(reducer);
store.subsribe(state => console.log(state)); // add function into listeners
store.dispatch(action);

Function with console.log will be called after state will changed. 在状态改变后将调用console.log函数。

That syntax is called an arrow function and is new to ECMAScript 6. Without the special syntax, that code would look like this: 该语法称为箭头函数 ,是ECMAScript 6的新增功能 。如果没有特殊语法,该代码将如下所示:

listeners.forEach(function(listener){
    return listener();
});

listener s is an array of functions. listener s是一个函数数组。 listeners is being iterated over with the Array.prototype.forEach function which takes a function as it's parameter. 侦听器正在使用Array.prototype.forEach函数进行迭代,该函数将函数作为其参数。

'listener' in that context is the variable name given to the function passed into the lambda expression. 该上下文中的'listener'是赋予传递给lambda表达式的函数的变量名。

When the Array.prototype.forEach function invokes your function, it passes in the current item in the iteration. 当Array.prototype.forEach函数调用您的函数时,它会传入迭代中的当前项。 in this code that item is a function and it's just being invoked. 在这段代码中,该项是一个函数,它只是被调用。

To put it simply, this code will iterate over an array of functions and invoke each one. 简单地说,这段代码将迭代一系列函数并调用每一个函数。

It might help to understand the API for the Array.prototype.forEach function. 理解Array.prototype.forEach函数的API可能会有所帮助。 Here's the documentation: 这是文档:

Array.prototype.forEach Array.prototype.forEach

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Arrow Functions 箭头功能

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Listen is a method on the store that takes a handler. Listen是商店中采用处理程序的方法。 The handler is a function that when called causes the DOM to render: 处理程序是一个函数,在调用时会导致DOM呈现:

 store.listen(() => { ReactDOM.render( <div>{JSON.stringify(store.getState(), null, 2)}</div>, document.getElementById('root') ); }); 

The way I understand it is that the listeners array consists of the render function on different React components that are connected to the Redux store. 我理解它的方式是侦听器数组包含连接到Redux存储的不同React组件上的render函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM