admin管理员组文章数量:1389829
I'm watching Dan Abramov's Redux videos on Egghead. In the video where he implements the Redux Store from scratch, he includes this code (around 1:28 - ):
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. Where does this function e from?
I'm watching Dan Abramov's Redux videos on Egghead. 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):
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. Where does this function e from?
Share Improve this question edited Mar 22, 2016 at 20:32 Kory Gill 7,1611 gold badge27 silver badges35 bronze badges asked Mar 22, 2016 at 19:28 anonanon 2,3635 gold badges29 silver badges37 bronze badges3 Answers
Reset to default 3createStore
method have listeners
variable. It is an array of functions which should be called when store updates.
You can add your own function into listeners
through the subscribe
method of store
.
For example:
const store = createStore(reducer);
store.subscribe(state => console.log(state)); // add function into listeners
store.dispatch(action);
Function with console.log
will be called after state will changed.
That syntax is called an arrow function and is new to ECMAScript 6. Without the special syntax, that code would look like this:
listeners.forEach(function(listener){
return listener();
});
listeners is an array of functions. listeners is being iterated over with the Array.prototype.forEach function which takes a function as it's parameter.
'listener' in that context is the variable name given to the function passed into the lambda expression.
When the Array.prototype.forEach function invokes your function, it passes in the current item in the iteration. 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. Here's the documentation:
Array.prototype.forEach
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
Arrow Functions
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Listen is a method on the store that takes a handler. The handler is a function that when called causes the DOM to render:
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 ponents that are connected to the Redux store.
本文标签: javascriptWhat listener() does in the Redux storeStack Overflow
版权声明:本文标题:javascript - What listener() does in the Redux store? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744730514a2622021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论