You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Redux Toolkit中createSlice的Action无法工作?求排查问题

Redux Toolkit Todo应用Action不生效问题排查

我正在使用Redux Toolkit开发Todo应用,应用内有三个逻辑近乎一致的Slice组件。所有组件的初始状态均可访问,但Action与Reducer无法生效;Redux DevTools能显示状态,却无法显示Action类型。不清楚问题所在,现将相关代码附上,请帮忙排查错误原因:

Store组件代码

import { configureStore } from '@reduxjs/toolkit';
import listsReducer from './listsSlice/listsSlice';
import tasksReducer from './tasksSlice/tasksSlice';
import currentListReducer from './currentListSlice/currentListSlice';

export default configureStore({
  reducer: {
      lists: listsReducer,
      tasks: tasksReducer,
      currentList: currentListReducer
  }
});

其中一个Slice组件代码

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

const currentListSlice = createSlice({
  name: 'currentList',
  initialState: {
     currentList: {}
  },
  reducers: {
    setCurrentList: (state, action) => {
        state.currentList = {
          ...action.payload.lists.filter(item => action.payload.id == item.id)
        };
    },

    clearCurrentList: (state) => {
        state.currentList = {};
    }
  }
});

export const selectCurrentList = state => state.currentList.currentList;
export const { setCurrentList, clearCurrentList } = currentListSlice.actions;
export default currentListSlice.reducer;

ButtonList组件代码

import React from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import { selectCurrentList } from '../../store/currentListSlice/currentListSlice';

function ButtonList(props) {
    const dispatch = useDispatch();

    return (
        <button className='listName'
            onClick={() => dispatch(selectCurrentList(props.id))}>
            <span className={`listColorIcon icon ${props.color}`}/>
            {props.listName}
        </button>
    );
}

ButtonList.propTypes = {
    onClick: PropTypes.func,
    listName: PropTypes.string,
    id: PropTypes.number,
    color: PropTypes.string
};

export default ButtonList;

错误原因排查

核心错误为:ButtonList组件中错误地dispatch了状态选择器selectCurrentList,而非对应的Action方法setCurrentList。

内容的提问来源于stack exchange,提问作者alienaline

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 23:45:42