为什么我的动作创建者没有在react redux app中定义?

时间:2017-08-31 07:39:28

标签: reactjs redux

我已经制作了组件通知,我希望它的动作和减速器可供所有应用程序使用。 但我不知道为什么我的行动没有定义。 这是代码

import { SHOW_NOTIFICATION, SHOW_NOTIFICATION_FULFILLED } from 'constants/actionTypes'

// notification
const initialState = {
  notification: {}
}

export function fetchNotification (data) {
  return {
    type: 'SHOW_NOTIFICATION',
    data: data
  }
}

export default function notificationReducer (state = initialState, action) {
  switch (action.type) {
    case SHOW_NOTIFICATION_FULFILLED: {
      return {
        ...state,
        notification: action.data
      }
    }
  }
  return state
}

SHOW_FULFILLED显示已定义但从未使用过如此错误

1 个答案:

答案 0 :(得分:0)

您使用SHOW_NOTIFICATION调用操作,但reducer仅处理SHOW_NOTIFICATION_FULFILLED。在reducer中使用与操作中相同的操作类型。

// notification
const initialState = {
  notification: {}
}

export function fetchNotification (data) {
  return {
    type: 'SHOW_NOTIFICATION', // <--
    data: data
  }
}

export default function notificationReducer (state = initialState, action) {
  switch (action.type) {
    case SHOW_NOTIFICATION: { // <--
      return {
        ...state,
        notification: action.data
      }
    }
  }
  return state
}