如何在 App 启动时使用 redux 启动 fetch?
How to start fetch with redux on App start?
设置一个简单的通知计数器后。我想在应用程序开始时获取通知以显示收到了多少通知。
配置如下:
减速器:
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
}
});
api端点是api地址
因此,在配置通知徽章组件并将其添加到路由器后,应用会加载帖子,因为是主屏幕而不是通知。
检查我将通知徽章组件作为主屏幕进行首次加载,但仍然没有收到通知。所以,请有人可以澄清如何在应用程序启动时或之前或之后使用 redux 获取?
export const Logged = createBottomTabNavigator(
{
Notification: {
screen: Notification,
navigationOptions: {
tabBarLabel: "Notification",
tabBarIcon: () => {
<NotificationIcon />;
}
}
},
好吧,有很多地方可能是错误的,首先我会使用像 postman, if the response is what you expect, then I would suggest creating a root component e.g AppComponent
and within it, in your componentDidMount
you can dispatch a getNotifications
action using mapDispatchToProps
这样的工具检查端点的响应
设置一个简单的通知计数器后。我想在应用程序开始时获取通知以显示收到了多少通知。
配置如下: 减速器:
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
}
});
api端点是api地址 因此,在配置通知徽章组件并将其添加到路由器后,应用会加载帖子,因为是主屏幕而不是通知。
检查我将通知徽章组件作为主屏幕进行首次加载,但仍然没有收到通知。所以,请有人可以澄清如何在应用程序启动时或之前或之后使用 redux 获取?
export const Logged = createBottomTabNavigator(
{
Notification: {
screen: Notification,
navigationOptions: {
tabBarLabel: "Notification",
tabBarIcon: () => {
<NotificationIcon />;
}
}
},
好吧,有很多地方可能是错误的,首先我会使用像 postman, if the response is what you expect, then I would suggest creating a root component e.g AppComponent
and within it, in your componentDidMount
you can dispatch a getNotifications
action using mapDispatchToProps