设置后一个简单的通知计数器。我想获取应用程序上的通知开始显示收到多少通知。
这是配置:还原器:
index.js
import { combineReducers } from "redux";
import posts from "./posts";
import filteredPosts from "./filteredPosts";
import notifications from "./notifications";
export default combineReducers({
posts,
filteredPosts,
notifications
});
Notification.js
import * as types from "../actions/types";
const initialState = [];
export default (state = initialState, action) => {
switch (action.type) {
case types.LOAD_NOTIFICATIONS:
return action.notifications;
default:
return state;
}
};
动作
type.js
export const FILTERED_POST = "FILTERED_POST";
export const LOAD_POSTS = "LOAD_POSTS";
export const LOAD_NOTIFICATIONS = "LOAD_NOTIFICATIONS";
Notification.js
import * as types from "./types";
import { fetchNotifications } from "../service/";
export const getNotifications = auth_token => dispatch =>
fetchNotifications(auth_token)
.then(res => {
return res.json();
})
.then(data => {
dispatch({
type: types.LOAD_NOTIFICATIONS,
notifications: data.notifications
});
})
.catch(err => {
console.log(err);
});
和服务
index.js
import { apiEndpoint } from "./config";
export const fetchPosts = auth_token =>
fetch(`${apiEndpoint}posts`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Access: auth_token
}
});
export const fetchNotifications = auth_token =>
fetch(`${apiEndpoint}notifications`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Access: auth_token
}
});
apiendpoint是API地址因此,在配置通知徽章组件并将其添加到路由器之后,应用程序加载帖子,因为是主屏幕,而不是通知。
要检查我将通知徽章组件作为首先加载的主屏幕,但仍未收到通知。因此,请有人可以澄清如何在应用程序开始或之前或之后使用Redux?
export const Logged = createBottomTabNavigator(
{
Notification: {
screen: Notification,
navigationOptions: {
tabBarLabel: "Notification",
tabBarIcon: () => {
<NotificationIcon />;
}
}
},
嗯,有很多事情可能是错误的,首先,我会使用诸如Postman之类的工具来检查端点的响应,如果响应是您的期望,那么我会建议创建一个根组件,例如AppComponent
,并且在其中,在componentDidMount
中,您可以使用MapDisPatchToprops
getNotifications
操作