【问题标题】:Reducer is not a function. React Native Redux减速器不是一个函数。反应原生 Redux
【发布时间】:2022-01-04 14:14:11
【问题描述】:

我正在尝试将减速器添加到我的 react 本机应用程序中。 这是存储常量:

export const USER_PROFILE = 'USER_PROFILE';

这里是 action.js

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

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

这是导致错误的 userReducer。我不断收到 customerReducer is not a function 的错误。

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

const initialState = {
  userProfile: '',
};

const customerReducer = (state = initialState, action) => {
  switch (action.type) {
    case USER_PROFILE:
      return {
        ...state,
        userProfile: action.payload,
      };
    default:
      return state;
  }
};

export default customerReducer;

我已将电子邮件声明为状态...const [email, setEmail] useState('')

在这里调用reducer。 const customerReducer = useSelector(state => state.userProfile); 现在使用 useDispatch 方法调度它。

const dispatch = useDispatch();
...
dispatch(customerReducer(email));

【问题讨论】:

标签: javascript react-native redux react-redux redux-toolkit


【解决方案1】:

你的 action.js 应该看起来像这样:

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

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

当你派出你应该使用动作“userProfile”而不是reducer“customerReducer”。您的调度应如下所示:

dispatch(userProfile({email}));

确保使用大括号。因为你应该传递一个对象而不是字符串。

【讨论】:

  • 谢谢你解决了它......
猜你喜欢
  • 2017-09-07
  • 2020-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多