如何从redux-saga select()获得状态?

问题描述 投票:-1回答:1

如下所示的传奇。

function *callUserAuth(action) {
    const selectAllState = (state) => state;
    const tmp = yield select(selectAllState);
    console.log(tmp);
}

控制台显示enter image description here

我如何获得像在redux中的getState [“ userLoginReducer”,“ isLogin”]吗?

我曾尝试像下面这样编码。

const tmp = yield select(selectAllState._root.entries);

但错误是

index.js:1 TypeError: Cannot read property 'entries' of undefine
select redux-saga
1个回答
0
投票

似乎您正在将Immutable.js用于您的redux状态。

select效果不会将您的不可修改结构转换为纯JavaScript。因此,您需要使用Immutable方法来获取所需的值。要获取整个不可变状态,然后将其转换为简单的javascript对象,您可以执行以下操作:

function *callUserAuth(action) {
    const selectAllState = (state) => state;
    const tmp = yield select(selectAllState);
    console.log(tmp.toJS());
}

但是通常,您可能希望有选择器来获得像isLogin值的子集。在这种情况下,您可以改为:

function *callUserAuth(action) {
    const getIsLogin = (state) => state.get('userLoginReducer').get('isLogin');
    const isLogin = yield select(getIsLogin);
    console.log(isLogin);
}
© www.soinside.com 2019 - 2024. All rights reserved.