简体   繁体   English

即使 state 已正确更新,UseSelector 也会返回 undefined

[英]UseSelector returns undefined even the state is updated correctly

I have a react admin project using React Redux.我有一个使用 React Redux 的 React 管理项目。

Below is the detail:以下是详细信息:

slice.tsx:

import {createSlice} from "@reduxjs/toolkit";

const newRevisionSlice = createSlice({
    name: 'newRevision',
    initialState: {
      dataList: [],
      status: 'idle'
    },
  reducers: {
    postSaveNewRevision: (state  = { dataList:[], status:"idle" }, action) => {
      return { ...state, dataList: action.payload, status: 'success' };

      }
    }
  });


export const { postSaveNewRevision } = newRevisionSlice.actions;
export default newRevisionSlice.reducer;
component.tsx: 

const newRevision  = useSelector((state: RootState) => state.newRevision)

store.tsx:

declare global {
  interface Window {
    __REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
  }
}

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const rootReducer = combineReducers({
  fleetType: fleetTypeApi,
  revisionNo: revisionNoApi,
  revisionDate:revisionDateApi,
  newRevision: newRevisionApi,
});

const store = createStore(rootReducer, composeEnhancers(applyMiddleware(thunk)));

const useAppDispatch = () => store.dispatch;

export type RootState = ReturnType<typeof store.getState>;

export { useAppDispatch };

ReactDOM.render(
  <Provider store={store}>
    <React.StrictMode>
      <App />
    </React.StrictMode>
  </Provider>,
  document.getElementById('root')
);

In react redux devtool I see that the state is updated correctly when I dispatch.在 react redux devtool 中,我看到 state 在我发送时已正确更新。 However, useSelector so newRevision in the component returns undefined.但是,组件中的 useSelector 使 newRevision 返回 undefined。

Could you please help me?请你帮助我好吗?

Thanks in advance.提前致谢。

Is there by any chance your newRevisionSlice and the newRevisionApi in store.jsx are 2 different reducers?您的newRevisionSlicenewRevisionApi中的store.jsx是否有可能是 2 个不同的减速器? If that is the case, then you have 2 separate stores for newRevision and they won't share states.如果是这种情况,那么您有 2 个单独的newRevision商店,它们不会共享状态。

Similar issues that are resolved:已解决的类似问题:

Additionally, because your useSelector() return undefined it could also be the case that there is a typo somewhere with the names of slices / reducers.此外,因为您的useSelector()返回undefined ,所以也可能是切片/缩减器名称某处存在拼写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM