如何使用redux分派我的变量?
我是新手。 我意识到我需要调度来保存存储中的变量。 但是,我无法分派设备\u令牌。 我试过“商店快件” 这是本机代码。 然而,我认为这是唯一的redux问题。 我需要帮助 我的代码如下如何使用redux分派我的变量?,redux,redux-observable,Redux,Redux Observable,我是新手。 我意识到我需要调度来保存存储中的变量。 但是,我无法分派设备\u令牌。 我试过“商店快件” 这是本机代码。 然而,我认为这是唯一的redux问题。 我需要帮助 我的代码如下 // @flow import { Component } from 'react'; import { Platform } from 'react-native'; import FCM, { FCMEvent, RemoteNotificationResult, WillPresentNotificati
// @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;
}
};
你知道吗?
我无法发送我的设备\u令牌。
谢谢。连接的第一个参数是
MapStateTops
。如果您不需要将任何状态映射到道具,只需将其更改为:
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);
});
});
}
请看地图
我认为您也没有正确地将
设备\u令牌
传递给getDeviceToken
操作创建者。尝试将mapDispatchToProps
更改为
const mapDispatchToProps = (dispatch: Dispatch) => {
return {
saveDeviceToken: (token) => dispatch(getDeviceToken({ device_token: token })),
};
};
谢谢你,迈克尔。但是,我得到了错误“未定义不是对象(评估'state.currentUser.payload.device_token'),我需要修复另一个错误。真的thanks@sonicmario你好如果他的回答确实解决了这个问题,请勾选接受,其他后续问题应在不同的SO问题中提出。谢谢它不起作用。在我的控制台上看不到GET_DEVICE_令牌。@sonicmario我已用另一个潜在问题更新了我的答案。似乎有一些问题可能会导致级联故障。值得一提的是,
state.currentUser
不会出现在您共享的代码中的任何地方,因此我最初的回答似乎让您通过了直接的障碍,但您遇到了一个新问题。它将保存到redux存储区的何处?