Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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_Unsubscribe - Fatal编程技术网

Javascript Redux-取消订阅侦听器如何工作?

Javascript Redux-取消订阅侦听器如何工作?,javascript,redux,unsubscribe,Javascript,Redux,Unsubscribe,我是新手。 我想知道什么是unsubscribe listener,它是如何工作的? 我知道register函数返回unsubscribe,但在下面的示例中,当我们调用unsubscribe方法时,为什么它不只是触发嵌套在变量中的新函数?我们可以看到: let unsubscribe = store.subscribe(() => { // let unsubscribe nests a function // execute every time the sta

我是新手。 我想知道什么是unsubscribe listener,它是如何工作的? 我知道register函数返回unsubscribe,但在下面的示例中,当我们调用unsubscribe方法时,为什么它不只是触发嵌套在变量中的新函数?我们可以看到:

let unsubscribe = store.subscribe(() => {  
    // let unsubscribe nests a function   
    // execute every time the state changes
    const state = store.getState(); 
});
// but here instead of call the nested function it cancels the listener, how is it possible ?
unsubscribe(); 

谢谢

redux中的unsubscribe函数实际上是在redux createStore方法中的subscribe方法中实现的。以下是它的工作原理:

//Inside store
    const subscribe = (listener) => {
           listeners.push(listener);
           return () => {
           listeners.filter(l => l !== listener);
        };
        };
// End of inside store 

const unsubscribe = store.subscribe(handleChange)
unsubscribe()

redux中的unsubscribe函数实际上是在redux createStore方法中的subscribe方法中实现的。以下是它的工作原理:

//Inside store
    const subscribe = (listener) => {
           listeners.push(listener);
           return () => {
           listeners.filter(l => l !== listener);
        };
        };
// End of inside store 

const unsubscribe = store.subscribe(handleChange)
unsubscribe()

我们正在调用store.subscribe()并向其传递回调函数。只要存储发生更改,就会调用该回调

函数签名解释了subscribe期望在每次分派时调用一个函数,并返回一个调用时将取消侦听器订阅的函数。想象一下:

function store.subscribe(callback){ 
    //execute the callback function
    callback();
    return store.unsubscribe()
}

我们正在调用store.subscribe()并向其传递回调函数。只要存储发生更改,就会调用该回调

函数签名解释了subscribe期望在每次分派时调用一个函数,并返回一个调用时将取消侦听器订阅的函数。想象一下:

function store.subscribe(callback){ 
    //execute the callback function
    callback();
    return store.unsubscribe()
}

我认为现在回答这个问题有点晚了,但更清楚地说,我想提出这个问题。 要记住的关键是:

  • 什么是
    store.subscribe
    ,以youtube
    subscribe
    选项(和钟形图标)的演示为例,现在每当频道管理员上传新视频时,它会立即在此处呼叫侦听器(即subscribe)并通知您,如果您取消订阅,则不会收到通知。很简单
  • store.subscribe
    ,或者说只要由于调度的操作而更改了状态,就会调用侦听器函数
  • subscribe函数的返回类型同样是
    取消订阅
    更改侦听器的函数
  • //操作是具有类型属性的对象
    const BUY_蛋糕=‘BUY_蛋糕’;
    //Action creator是一个返回操作的函数
    函数buyCake(){
    返回{
    类型:买蛋糕,
    信息:“第一次重复操作”,
    }
    }
    //reducer的初始状态,reducer->(以前的状态,操作)=>newState
    常量初始状态={
    月饼:10
    }
    //这是我们的减速机功能
    const reducer=(state=initialState,action)=>{
    开关(动作类型){
    case BUY_蛋糕:退货{
    …状态,//制作状态对象的副本
    numOfCakes:state.numOfCakes-1
    }
    默认:返回状态;
    }
    }
    const store=createStore(reducer);
    log(“初始状态”,store.getState());
    /*返回应用程序的当前状态树。它等于存储的减缩器返回的最后一个值*/
    //每次调度操作时都将调用订阅侦听器
    const unsubscribe=store.subscribe(()=>console.log(“更新状态”,store.getState()))
    store.dispatch(buyCake());
    store.dispatch(buyCake());
    取消订阅();
    store.dispatch(buyCake());
    
    log(“取消订阅后的状态”,store.getState())我认为现在回答这个问题有点晚了,但更清楚地说,我想介绍一下。 要记住的关键是:

  • 什么是
    store.subscribe
    ,以youtube
    subscribe
    选项(和钟形图标)的演示为例,现在每当频道管理员上传新视频时,它会立即在此处呼叫侦听器(即subscribe)并通知您,如果您取消订阅,则不会收到通知。很简单
  • store.subscribe
    ,或者说只要由于调度的操作而更改了状态,就会调用侦听器函数
  • subscribe函数的返回类型同样是
    取消订阅
    更改侦听器的函数
  • //操作是具有类型属性的对象
    const BUY_蛋糕=‘BUY_蛋糕’;
    //Action creator是一个返回操作的函数
    函数buyCake(){
    返回{
    类型:买蛋糕,
    信息:“第一次重复操作”,
    }
    }
    //reducer的初始状态,reducer->(以前的状态,操作)=>newState
    常量初始状态={
    月饼:10
    }
    //这是我们的减速机功能
    const reducer=(state=initialState,action)=>{
    开关(动作类型){
    case BUY_蛋糕:退货{
    …状态,//制作状态对象的副本
    numOfCakes:state.numOfCakes-1
    }
    默认:返回状态;
    }
    }
    const store=createStore(reducer);
    log(“初始状态”,store.getState());
    /*返回应用程序的当前状态树。它等于存储的减缩器返回的最后一个值*/
    //每次调度操作时都将调用订阅侦听器
    const unsubscribe=store.subscribe(()=>console.log(“更新状态”,store.getState()))
    store.dispatch(buyCake());
    store.dispatch(buyCake());
    取消订阅();
    store.dispatch(buyCake());
    log(“取消订阅后的状态”,store.getState())