我有一個可供選擇的“比較器”列表。我想在標題下方顯示一個比較器中的帳戶數(shù)量。我能夠創(chuàng)建一個函數(shù)來獲取帳戶并獲取大小。但是,它只在我第二次加載頁面時有效。如果我第一次加載它,結果總是顯示 0。我如何確保在我第一次加載頁面時,數(shù)據(jù)被正確獲取和加載?import { SET_COMPARATOR_PRODUCTS_AMOUNT, GET_COMPARATORS_INSTITUTIONS, GET_COMPARATOR_ACCOUNTS,} from '../types/Comparators';import Config from 'react-native-config';import {AxiosInstance} from '../api/AxiosInstance';export const getAmount = comparator => dispatch => { AxiosInstance.get('/comparators/' + comparator, { headers: { 'X-Comparator-API-KEY': Config.COMPARATOR_API_KEY, 'content-type': 'application/json', }, }).then(res => { dispatch({ type: SET_COMPARATOR_PRODUCTS_AMOUNT, payload: { comparator: comparator, amount: res.data.length, }, }); });};import { SET_COMPARATOR_PRODUCTS_AMOUNT, GET_COMPARATORS_INSTITUTIONS, GET_COMPARATOR_ACCOUNTS,} from '../types/Comparators';const INITIAL_STATE = { accountsAmount: {}, institutions: [], accounts: [],};export default (state = INITIAL_STATE, action) => { switch (action.type) { case SET_COMPARATOR_PRODUCTS_AMOUNT: var accountsAmount = state.accountsAmount; accountsAmount[action.payload.comparator] = action.payload.amount; return Object.assign({}, state, {accountsAmount: accountsAmount}); case GET_COMPARATORS_INSTITUTIONS: return { ...state, institutions: action.payload, }; case GET_COMPARATOR_ACCOUNTS: return { ...state, accounts: action.payload, }; default: return state; }};
為什么道具/動作不是第一次通過?
慕絲7291255
2022-12-18 18:58:19