Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.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/3/xpath/2.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 避免对Saga不需要的请求_Javascript_Reactjs_Redux_Redux Saga - Fatal编程技术网

Javascript 避免对Saga不需要的请求

Javascript 避免对Saga不需要的请求,javascript,reactjs,redux,redux-saga,Javascript,Reactjs,Redux,Redux Saga,我有一个redux saga函数绑定到“@@router/LOCATION\u CHANGE”操作。主要问题是,每次用户在分区之间切换时,都会有大量不必要的请求,因为存储中已经存在数据 export function* resourcesSwitcher(action) { yield put(getConstants()); switch (section) { case '/dashboard': yield all([

我有一个redux saga函数绑定到“@@router/LOCATION\u CHANGE”操作。主要问题是,每次用户在分区之间切换时,都会有大量不必要的请求,因为存储中已经存在数据

export function* resourcesSwitcher(action) {
    yield put(getConstants());

    switch (section) {
        case '/dashboard':
        yield all([
            put((check) ? getDate() : addDate(date)),
        ]);
        break;
        case '/apps':
        yield all([
            put(getApps({ filter })),
            put(getPlatforms()),
            roleId >= C.roles.User && put(getKeys({ filter })),
        ]);
        break;
        case '/rules':
        yield all([
            put(getRules({ filter: rulesFilter })),
            put(getKeys({ filter })),
            put(getDevices()),
        ]);
        break;
        case '/revenues':
        yield all([
            put((check) ? getDate() : addDate(date)),
            put(getApps()),
            put(getAccounts()),
        ]);
        break;
        case '/keys':
        yield all([
            put(getKeys({ filter })),
            put(getApps()),
        ]);
        break;
        default:
    }
}

export default function* root() {
    yield all([
        takeLatest('FETCH', resourcesSwitcher),
    ]);
}
我想添加一些checker函数来确保store[value]不是空的,这样saga就可以避免调用。f、 e如果数据已经存在,则不在每个位置请求常量或应用程序\u更改操作

const getStore = (state) => state;
const store = yield select(getStore);

function* checker(value) {
    console.log('checking store', value);
    let result = null;
    let method = null;
    switch (value) {
    case 'apps':
        method = getApps();
        break;
    case 'keys':
        method = getKeys();
        break;
    case 'constants':
        method = getConstants();
        break;
    default:
        result = (store[value].data.length) ? null : method;
    }
    if (result) return yield put(result);
    console.log('prefetched, avoided');
    return {};
}

我没有完全掌握redux传奇,因此可能是实现这种“缓存”的更好方法。

对于这种情况,我通常会编写一个选择器,并在进行api调用之前检查它。比如:

// adapt this selector based on your store structure and conditions
// for fetching data again
const shouldFetchConstantsSelector = state => state.constants.data.length > 0;

export function* resourcesSwitcher(action) {
    const shouldFetchConstants = yield select(shouldFetchConstantsSelector);
    if (shouldFetchConstants) {
      yield put(getConstants());
    }
    ... etc.
}