简体   繁体   English

Redux-Saga如何在Saga中调用localStorage.clear方法

[英]Redux-Saga how to call localStorage.clear method in Saga

I am trying to clear all items stored in localStorage in Redux Saga method. 我正在尝试以Redux Saga方法清除存储在localStorage中的所有项目。 But it's not working as expected. 但是它没有按预期工作。

In theory, If I want to call a function in Saga, we need to write it without brackets with call keyword. 从理论上讲,如果我想在Saga中调用函数,则需要使用call关键字将其编写为不带括号的函数。

So, I tried to write it with yield call(localStorage.clear); 因此,我尝试用yield call(localStorage.clear);编写它yield call(localStorage.clear); but it's not clearing items from the LocalStorage. 但它不会清除LocalStorage中的项目。 If I added brackets () or without yeild & call , it's working and clearing items in LocalStorage as expected. 如果我添加了brackets ()或不添加yeild & call ,它将按预期工作并清除LocalStorage中的项目。

export function* logoutUserSaga() {
try {
    const accessToken = yield call(AuthService.getAccessToken);
    yield call(AuthService.logoutUser, accessToken); 
    yield put(logoutUser.success());

    yield call(localStorage.clear); // not working

    //yield call(localStorage.clear()); // working
    //localStorage.clear(); // working

    yield put({ type: RESET_ALL_STATE });
}
catch (error) {
    yield put(logoutUser.failure({ errorMessage: error.statusText }));
}
}

export default function* watcherSaga() {
    yield takeLatest(authenticateUser.TRIGGER, authenticateUserSaga);
    yield takeLatest(logoutUser.TRIGGER, logoutUserSaga);
    yield takeLatest(getAccessToken.TRIGGER, getAccessTokenSaga);
}

I would like to know why the call function without brackets () is not working. 我想知道为什么不带方括号()的调用函数不起作用。

Is it because the function being called is void and not returning any value? 是否因为被调用的函数无效且不返回任何值?
Do we always have to add brackets if we want to call void methods? 如果要调用void方法,是否总是必须添加方括号吗?

The reason it's not working is that the localStorage.clear function expects this to be equal to localStorage . 它不起作用的原因是localStorage.clear函数期望this等于localStorage That happens automatically when using the notation localStorage.clear , but if you just have a reference to the function and call it without context, you get an Illegal invocation error. 当使用符号localStorage.clear ,这种情况会自动发生,但是如果您仅引用了该函数并在没有上下文的情况下调用它,则会收到非法调用错误。 This is not directly related to sagas, and can be reproduced like this: 这与sagas没有直接关系,可以这样复制:

  const clear = localStorage.clear;
  clear(); // throws an exception

This does have an indirect relationship to sagas though, which is the way call works. 但这确实与sagas有间接关系,这就是call工作方式。 If you don't tell call what context it should use when calling the function, then it has no choice but to call it in a global context, causing this exception. 如果在调用函数时不告诉调用它应该使用什么上下文,那么它别无选择,只能在全局上下文中调用它,从而导致此异常。 call does have a few variations on its parameters that let you specify what this should equal. call确实有它的参数,让你指定哪几个变化this应该相等。 For example, you could do this: 例如,您可以这样做:

yield call([localStorage, localStorage.clear]);

You can see other variations of the parameters that call accepts here: https://redux-saga.js.org/docs/api/ 您可以在此处查看调用接受的参数的其他变体: https : //redux-saga.js.org/docs/api/


Another option is to just not use call . 另一种选择是不使用call Using call has benefits when trying to test a saga, and has the benefit that it works with both sagas and normal functions, but you can still call normal functions yourself if you want. 在尝试测试传奇时,使用call会有好处,并且具有可以在sagas和常规函数中使用的优点,但是您仍然可以自己调用常规函数。

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

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