Redux Thunk getState() 正在返回所有状态
Redux Thunk getState() is returning all states
刚刚发现我可以使用 getState() 将状态值传递给动作创建者。但是我注意到 getState() 正在返回所有组合状态,而不是参数中指定的状态。我做错了什么,因为我不认为这是正确的行为?
减速器:
import { combineReducers } from "redux";
import { reducer as reduxForm } from "redux-form";
import authReducer from "./authReducer";
export default combineReducers({
auth: authReducer,
form: reduxForm
});
动作创作者片段:
export const handleProviderToken = token => async (dispatch, getState) => {
let testState = getState("auth");
console.log(testState);
const res = await axios.get(`${API_URL}/api/testEndpoint`);
dispatch({ type: FETCH_USER, payload: res.data });
};
console.log(testState) 向我展示了整个状态树对象(auth 和 form,而不仅仅是 auth)。
这是正确的行为。 From docs:
Returns the current state tree of your application. It is equal to the
last value returned by the store's reducer.
这是正确的行为。您需要从完整状态中选择 reducer 键。
export const handleProviderToken = token => async (dispatch, getState) => {
let testState = getState();
let authReducer = testState.auth;
const res = await axios.get(`${API_URL}/api/testEndpoint`);
dispatch({ type: FETCH_USER, payload: res.data });
};
引用 redux-thunk 文档
The inner function receives the store methods dispatch and getState as
parameters.
并引用 redux 的文档
getState() Returns the current state tree of your application. It is
equal to the last value returned by the store's reducer.
所以你通过 redux-thunk 传递的 getState 实际上是 redux 的 getState() 函数本身,因此它是默认行为。
要从状态树中获取特定值,您可以使用以下方法之一
const { auth } = getState()
// OR
const testState = getState()
const auth = testState.auth
在类似情况下对我有用的是
const lastFetchedPage =getState().images.lastFetchedPage;
但是,我不知道它与指南的关系如何。
刚刚发现我可以使用 getState() 将状态值传递给动作创建者。但是我注意到 getState() 正在返回所有组合状态,而不是参数中指定的状态。我做错了什么,因为我不认为这是正确的行为?
减速器:
import { combineReducers } from "redux";
import { reducer as reduxForm } from "redux-form";
import authReducer from "./authReducer";
export default combineReducers({
auth: authReducer,
form: reduxForm
});
动作创作者片段:
export const handleProviderToken = token => async (dispatch, getState) => {
let testState = getState("auth");
console.log(testState);
const res = await axios.get(`${API_URL}/api/testEndpoint`);
dispatch({ type: FETCH_USER, payload: res.data });
};
console.log(testState) 向我展示了整个状态树对象(auth 和 form,而不仅仅是 auth)。
这是正确的行为。 From docs:
Returns the current state tree of your application. It is equal to the last value returned by the store's reducer.
这是正确的行为。您需要从完整状态中选择 reducer 键。
export const handleProviderToken = token => async (dispatch, getState) => {
let testState = getState();
let authReducer = testState.auth;
const res = await axios.get(`${API_URL}/api/testEndpoint`);
dispatch({ type: FETCH_USER, payload: res.data });
};
引用 redux-thunk 文档
The inner function receives the store methods dispatch and getState as parameters.
并引用 redux 的文档
getState() Returns the current state tree of your application. It is equal to the last value returned by the store's reducer.
所以你通过 redux-thunk 传递的 getState 实际上是 redux 的 getState() 函数本身,因此它是默认行为。
要从状态树中获取特定值,您可以使用以下方法之一
const { auth } = getState()
// OR
const testState = getState()
const auth = testState.auth
在类似情况下对我有用的是
const lastFetchedPage =getState().images.lastFetchedPage;
但是,我不知道它与指南的关系如何。