reducer A 使 reducer B 未定义(React - Redux)

reducer A make reducer B undefined (React - Redux)

我在组合减速器中有两个减速器,当我调用一个操作来更改 reducerSrc 的状态时,我的 reducerTherm 状态变得未定义并且我收到一个错误。我该如何解决这个问题?

合并:

 const rootReducer = combineReducers({
      therm: reducerTherm,
      src: reducerSrc,
    });
    
    export type RootState = ReturnType<typeof rootReducer>;
    
    export const store = createStore(rootReducer);

reducerTherm:

interface Props {
  therm: string;
}

const initialState: Props = {
  therm: "",
};

export default function reducerTherm(state: Props = initialState, action: any) {
  switch (action.type) {
    case "SEARCH_THERM":
      return action.payload;
      break;
    default:
      return state.therm;
  }
}

reducerSrc:

export interface Props {
  src: any;
}

const initialState: Props = {
  src: "source teste",
};

export default function reducerSrc(state: Props = initialState, action: any) {
  switch (action.type) {
    case "ADD_SRC":
      return action.payload;
      break;
    default:
      return state;
  }
}

useEffect 观察热变化并激活对 reducerSrc 的操作:

 const therm = useSelector((state: RootState) => state.therm);
  const src = useSelector((state: RootState) => state.src);

  useEffect(() => {
    if (therm) {
      try {
        getPhotosPlaces("shopping")
          .then((res) => res.json())
          .then((data) => {
            store.dispatch({
              type: "ADD_SRC",
              payload: data,
            });
          });
      } catch (error) {
        console.log(error);
      }
    }
  }, [therm]);

错误:

**Unhandled Rejection (Error): When called with an action of type "ADD_SRC", the slice reducer for key "therm" returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.**

在你的热减速器的默认情况下,你有

return state.therm; // possibly undefined

您肯定是要返回 reducer 状态(不是密钥 therm

return state; // returns { therm: ... }