在 redux-saga 中获取状态? [英] getState in redux-saga?

查看:60
本文介绍了在 redux-saga 中获取状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一家商店,里面有商品清单.当我的应用程序第一次加载时,我需要反序列化这些项目,就像基于这些项目创建一些内存中的对象一样.这些项目存储在我的 redux 存储中并由 itemsReducer 处理.

I have a store with a list of items. When my app first loads, I need to deserialize the items, as in create some in-memory objects based on the items. The items are stored in my redux store and handled by an itemsReducer.

作为副作用,我正在尝试使用 redux-saga 来处理反序列化.在第一页加载时,我调度一个动作:

I'm trying to use redux-saga to handle the deserialization, as a side effect. On first page load, I dispatch an action:

dispatch( deserializeItems() );

我的传奇设置很简单:

function* deserialize( action ) {
    // How to getState here??
    yield put({ type: 'DESERISLIZE_COMPLETE' });
}

function* mySaga() {
    yield* takeEvery( 'DESERIALIZE', deserialize );
}

在我的反序列化传奇中,我想处理创建项目的内存版本的副作用,我需要从存储中读取现有数据.我不知道如何在这里做到这一点,或者如果这是我什至应该尝试使用 redux-saga 的模式.

In my deserialize saga, where I want to handle the side effect of creating in-memory versions of my items, I need to read the existing data from the store. I'm not sure how to do that here, or if that's a pattern I should even be attempting with redux-saga.

推荐答案

你可以使用 选择效果

you can use select effect

import {select, ...} from 'redux-saga/effects'

function* deserialize( action ) {
    const state = yield select();
    ....
    yield put({ type: 'DESERIALIZE_COMPLETE' });
}

您也可以将它与选择器一起使用

also you can use it with selectors

const getItems = state => state.items;

function* deserialize( action ) {
    const items = yield select(getItems);
    ....
    yield put({ type: 'DESERIALIZE_COMPLETE' });
}

这篇关于在 redux-saga 中获取状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆