如何使用redux调度我的变量?

时间:2018-02-15 05:03:12

标签: redux redux-observable

我是redux的新手。 我意识到我需要派遣来保存我们商店中的变量。 但是,我无法发送device_token。 我试过“store.dispatch”。

它是反应原生代码。 但是,我认为这是唯一的还原问题。 我需要帮助。

我的代码如下。

// @flow
import { Component } from 'react';
import { Platform } from 'react-native';
import FCM, { FCMEvent, RemoteNotificationResult, 
WillPresentNotificationResult, NotificationType } from 'react-native-fcm';
import { connect } from 'react-redux';
import { getDeviceToken } from '../common/CurrentUser/actions';

const showLocalNotification = (notif) => {
  FCM.presentLocalNotification({
    title: notif.title,
    body: notif.body,
    priority: 'high',
    click_action: notif.click_action,
    show_in_foreground: true,
    local: true,
  });
};

export class PushNotification extends Component {
  constructor(){
    super();
  }

componentDidMount() {
  FCM.requestPermissions().then( () => {
    FCM.getFCMToken().then((token: string) => {
      const device_token = token;
      const { saveDeviceToken } = this.props;
      alert(device_token);
      dispatch(saveDeviceToken(device_token))
      //dispatch(getDeviceToken( { device_token }));
      alert(token);
    });
  });

  FCM.on(FCMEvent.RefreshToken, token => {
    alert(token);
  });


  this.notificationListner = FCM.on(FCMEvent.Notification, (notif: Object) => {
  if (notif.local_notification) {
    return;
  }
  if (notif.opened_from_tray) {
    return;
  }

  if (Platform.OS === 'ios') {
    switch (notif._notificationType) {
      case NotificationType.Remote:
        notif.finish(RemoteNotificationResult.NewData);
        break;
      case NotificationType.NotificationResponse:
        notif.finish();
        break;
      case NotificationType.WillPresent:
        notif.finish(WillPresentNotificationResult.All);
        break;
      default:
        break;
      }
    }
      showLocalNotification(notif);
    });
  }

  componentWillUnmount() {
    this.notificationListner.remove();
    this.refreshTokenListener.remove();
  }

  render() {
    return null;
  }
}

const mapDispatchToProps = (dispatch: Dispatch) => {
  return {
    saveDeviceToken: (token) => dispatch(getDeviceToken(token)),
  };
};

export default connect(mapDispatchToProps)(PushNotification);

// action.types.js

// @flow
import type { Action } from './actionTypes';
import type { User } from '../../entities';

export const SUCCESS_LOGIN = 'SUCCESS_LOGIN';
export const GET_DEVICE_TOKEN = 'GET_DEVICE_TOKEN';

export const successLogin = (payload: { user: User, authToken: string }): Action =>
  ({ type: 'SUCCESS_LOGIN', payload });

export const getDeviceToken = (payload: { device_token: string }): Action =>
  ({ type: 'GET_DEVICE_TOKEN', payload });

// reducer.js

export default (state: State = INITIAL_STATE, action: Action): State => {
  switch (action.type) {
  case 'GET_DEVICE_TOKEN':
    return {
      state,
    };
  default:
    return state;
  }
};

你有什么想法吗? 我无法发送我的device_token。 感谢。

1 个答案:

答案 0 :(得分:2)

connect的第一个参数是mapStateToProps。如果您不需要将任何状态映射到props,则可以将其更改为:

export default connect(null, mapDispatchToProps)(PushNotification);

这将绕过该参数并允许mapDispatchToProps作为第二个参数传递。

然后你可以像功能一样把它叫做道具

componentDidMount() {
  FCM.requestPermissions().then( () => {
   FCM.getFCMToken().then((token: string) => {
      const device_token = token;
      const { saveDeviceToken } = this.props;
      saveDeviceToken(device_token);
    });
  });
}

请参阅API docs for more details

我认为您也没有正确地将device_token传递给getDeviceToken动作创建者。尝试将mapDispatchToProps更改为

const mapDispatchToProps = (dispatch: Dispatch) => {
  return {
    saveDeviceToken: (token) => dispatch(getDeviceToken({ device_token: token })),
  };
};