Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在redux商店内部,订阅(取消订阅)是如何工作的?_Javascript_Redux - Fatal编程技术网

Javascript 在redux商店内部,订阅(取消订阅)是如何工作的?

Javascript 在redux商店内部,订阅(取消订阅)是如何工作的?,javascript,redux,Javascript,Redux,在中,他重新创建了redux存储的基本实现 subscribe方法如下所示 const subscribe = (listener)=>{ listeners.push(listener) //subscribe return ()=>{ listeners = listeners.filter(l => l !== listener) //unsubscribe } } 每次调用dispatch时,它都会调用侦听器数组中的

在中,他重新创建了redux存储的基本实现

subscribe
方法如下所示

const subscribe = (listener)=>{
    listeners.push(listener) //subscribe
      return ()=>{
        listeners = listeners.filter(l => l !== listener) //unsubscribe
      }
  }
每次调用
dispatch
时,它都会调用
侦听器
数组中的所有函数

从redux文档:

要取消订阅更改侦听器,请调用subscribe返回的函数

const unsubscribe=store.subscribe(handleChange)


为什么要这样处理?这个函数对我来说似乎非常违反直觉,而且看起来比实现一个专用的
取消订阅
方法更复杂。

我认为它更像一个语法糖!我觉得它更像是一种语法糖!