redux 中切片的初始状态 returns 未定义

initialState of Slice in redux returns undefined

我收到此错误 Uncaught TypeError: Cannot read properties of undefined (reading 'subreddits') 它指的是我定义 selectSubreddit 的最后一行代码,我将 subreddit 初始状态设置为空对象,那么为什么未定义?有人可以给我解释一下吗

SubredditSlice.js

import { createSlice} from '@reduxjs/toolkit';
import { getSubreddits } from '../api';


const subredditSlice = createSlice({
    name: 'subreddits',
    initialState: {
        subreddits: {},
        isPending: false,
        isfailed: false,
    },
    reducers: {
        getSubredditsPending(state) {
            state.isPending = true;
            state.isfailed = false;
        },
        getSubredditsfulfilled(state, action) {
            state.subreddits = action.payload;
        },
        getSubredditsFailed(state) {
            state.isLoading = false;
            state.isfailed = true;
        }
    }
})

export const fetchSubreddits = () => async  (dispatch) => {
    try {
        dispatch(getSubredditsPending());
        const subreddits = await getSubreddits();
        dispatch(getSubredditsfulfilled(subreddits));
    } catch (error) {
        dispatch(getSubredditsFailed);
    }
} 

export const {getSubredditsPending, getSubredditsfulfilled, getSubredditsFailed} = subredditSlice.actions;

export default subredditSlice.reducer;
export  const selectSubreddit = (state) => state.subreddits.subreddits;

store.js

import { configureStore} from "@reduxjs/toolkit";
import subredditReducer from './subredditSlice';

export default configureStore({
    reducer: {
        subreddit: subredditReducer,
    }
})

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { Provider } from 'react-redux';
import  store  from './store/store';

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

您的选择器正在访问 state.subreddits.subreddits,但您在 configureStore 调用中将切片添加为 subreddit,因此它应该访问 state.subreddit.subreddits