Javascript 如何在应用程序启动时使用redux开始提取?
设置一个简单的通知计数器后。我想在应用程序开始时获取通知,以显示收到了多少通知 以下是配置: 减速器: index.jsJavascript 如何在应用程序启动时使用redux开始提取?,javascript,react-native,redux,react-redux,redux-thunk,Javascript,React Native,Redux,React Redux,Redux Thunk,设置一个简单的通知计数器后。我想在应用程序开始时获取通知,以显示收到了多少通知 以下是配置: 减速器: index.js import { combineReducers } from "redux"; import posts from "./posts"; import filteredPosts from "./filteredPosts"; import notifications from "./notifications"; export default combineReduce
import { combineReducers } from "redux";
import posts from "./posts";
import filteredPosts from "./filteredPosts";
import notifications from "./notifications";
export default combineReducers({
posts,
filteredPosts,
notifications
});
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
}
});
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;
}
};
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);
});
行动
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 "../actions/types";
const initialState = [];
export default (state = initialState, action) => {
switch (action.type) {
case types.LOAD_NOTIFICATIONS:
return action.notifications;
default:
return state;
}
};
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 { combineReducers } from "redux";
import posts from "./posts";
import filteredPosts from "./filteredPosts";
import notifications from "./notifications";
export default combineReducers({
posts,
filteredPosts,
notifications
});
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
}
});
api端点是api地址
因此,在配置通知徽章组件并将其添加到路由器后,应用程序将加载帖子,因为这是主屏幕,而不是通知
为了检查,我将notification badge组件作为第一次加载的主屏幕,但仍然没有收到通知。因此,请有人澄清如何在应用程序启动时或之前或之后获取使用redux
export const Logged = createBottomTabNavigator(
{
Notification: {
screen: Notification,
navigationOptions: {
tabBarLabel: "Notification",
tabBarIcon: () => {
<NotificationIcon />;
}
}
},
export const Logged=createBottomTabNavigator(
{
通知:{
屏幕:通知,
导航选项:{
tabBarLabel:“通知”,
tabBarIcon:()=>{
;
}
}
},
有很多事情可能是错误的,首先我会使用一个工具检查端点的响应,比如,如果响应是您期望的,那么我建议创建一个根组件,例如AppComponent
,在它里面,在componentDidMount
中,您可以发送getNotifications代码>操作使用感谢您的回答。那么,您的意思是获取appComponent上的通知,然后传递到NotificationIcon组件?是的。只需将数组的长度传递到NotificationIcon
组件。