是否可以在reducer内使用getState()?

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

登录还原器工作正常,但是当我添加注销还原器时,它不起作用。

export const rootReducer = combineReducers({
    login: loginReducer,
    logout: logoutReducer
});


export const loginReducer = (state = initialState, { type, payload } :any) => {
  switch (type) {
    case "login":
      return {...state, token: payload};
    default:
      return state;
  }
};

如果登录成功,我将运行graphql突变,将返回令牌,该令牌使用redux存储。以后,此信息用于进行专用路由等。

let submitForm = (email: string, password: string) => {
    setIsSubmitted(true);
    login({
      variables: {
        email: email,
        password: password,
      },
    })
      .then(({ data }: any) => {
      //.then(({ data }: ExecutionResult<MyDataReponse>) => {
        dispatch({ type: "login", payload: data.loginEmail.accessToken })

现在,我想添加一个注销减少器。如果单击按钮,我想注销。如果将其放在rootReducer中,它将编译而不会出现错误。

const initialState = {
  token: null
};
const storeValue = store.getState();

export const logoutReducer = (state = initialState, { type, payload } :any) => {
switch (type) {
  case "check":
    return {...state, token: payload};
  default:
    return state;
}
};

但是,如果我执行state = storeValue而不是initialState,则会在storeValue定义上出现'storeValue' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)的错误

以及logoutReducer那>

implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)```

登录减速器工作正常,但是当我添加注销减速器时,它不起作用。 export const rootReducer = CombineReducers({login:loginReducer,注销:logoutReducer}); export const ...

javascript reactjs typescript redux react-redux
1个回答
0
投票

经过一番思考(尚未经过测试确定),我认为答案是否定的。您不能在reducer内使用getState()。

© www.soinside.com 2019 - 2024. All rights reserved.