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

React Native Redux提示Reducer is not a function报错问题求助

报错核心原因
  1. 命名冲突导致变量覆盖:你在组件内通过useSelector获取状态时,直接将返回值命名为customerReducer,覆盖了原本的reducer函数引用。useSelector返回的是状态值而非函数,调用时自然会抛出「不是函数」的错误。
  2. 派发逻辑错误:dispatch接收的参数应为action对象,你错误地将被覆盖为状态值的customerReducer当作函数调用传入,完全不符合redux的执行逻辑。
  3. action生成函数逻辑缺陷:你定义的userProfile action生成函数接收了参数但未使用,payload内的email是硬编码的空字符串,传参不会生效。
  4. 基础语法错误:组件内声明useState时遗漏了等号,会导致单独的语法报错。
解决方案

1. 修正action生成函数逻辑

修改action.js代码,将传入的参数绑定到payload:

import {USER_PROFILE} from '../constants/index';

export function userProfile(email) {
  return {
    type: USER_PROFILE,
    payload: {
      email: email,
    },
  };
}

2. 检查根reducer注册

确保你在根reducer中正确注册了customerReducer,示例如下:

import { combineReducers } from 'redux';
import customerReducer from './path/to/customerReducer';

export default combineReducers({
  // 此处的key为后续useSelector取值的第一层字段
  customer: customerReducer
});

3. 修正组件内代码

调整命名、修复语法、修正派发逻辑:

import { useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
// 导入action生成函数
import { userProfile } from '../path/to/action';

// 组件内部逻辑
// 补全useState的等号语法
const [email, setEmail] = useState('');
// 调整状态变量命名,避免冲突,取值路径对应根reducer注册的key
const userProfileState = useSelector(state => state.customer.userProfile);
const dispatch = useDispatch();

// 派发时调用action生成函数传入参数
dispatch(userProfile(email));

内容的提问来源于stack exchange,提问作者Jason Byron Beedle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 16:24:00