React native 从函数访问数组,反应本机
我试图访问从API调用的数组元素。我已经设法调用了它,但是如何访问和使用数组中的值呢?我也无法设置对React native 从函数访问数组,反应本机,react-native,api,react-hooks,use-effect,React Native,Api,React Hooks,Use Effect,我试图访问从API调用的数组元素。我已经设法调用了它,但是如何访问和使用数组中的值呢?我也无法设置对setAccountDetails和setRecentTransactions的响应 下面是我的代码 import React, { useState, useEffect } from 'react'; import { StyleSheet, View, ScrollView, Text } from 'react-native'; import { Colors, Fonts } from
setAccountDetails
和setRecentTransactions
的响应
下面是我的代码
import React, { useState, useEffect } from 'react';
import { StyleSheet, View, ScrollView, Text } from 'react-native';
import { Colors, Fonts } from '../../styles/index';
import Header from '../../components/home/Header';
import BalanceCard from '../../components/home/BalanceCard';
import QuickActionsSection from '../../components/home/QuickActionsSection';
import {
GetAccountDetailsResponse,
TGetAccountDetailsResponse,
} from '../../models/home/AccountDetails';
import {
GetRecentTransactionResponse,
TGetRecentTransactionResponse,
} from '../../models/home/RecentTransaction';
import * as HomeServices from '../../api/HomeServices';
import { ThrowReporter } from 'io-ts/lib/ThrowReporter';
import { ApiErrorHandler } from '../../utilities/ApiErrorHandler';
import RecentTransactions from '../../components/home/RecentTransactions';
import OnloadRecentTransactionsAPI from '../../components/home/OnloadRecentTransactionsAPI';
export default function Home() {
const [accountDetails, setAccountDetails] = useState([]);
const [recentTransactions, setRecentTransactions] = useState([]);
const getAccountDetails = async () => {
const resultAccountDetails = await HomeServices.getAccountDetails();
const responseAccountDetails = resultAccountDetails.body as GetAccountDetailsResponse;
ThrowReporter.report(
TGetAccountDetailsResponse.decode(responseAccountDetails),
);
return responseAccountDetails;
};
const getRecentTransactions = async () => {
const resultRecentTransactions = await HomeServices.getRecentTransaction();
const responseRecentTransactions = resultRecentTransactions.body as GetRecentTransactionResponse;
ThrowReporter.report(
TGetRecentTransactionResponse.decode(responseRecentTransactions),
);
return responseRecentTransactions;
};
useEffect(() => {
try {
getAccountDetails(); //Call API here
getRecentTransactions(); //Call API here
} catch (e) {
ApiErrorHandler(e);
}
}, []);
//Want to use 'balance' from the array in the API
function totalBalance(accountDetails: any) {
let totalBalance = 0;
let total = accountDetails.accounts;
for (var i = 0; i < total.length; i++) {
totalBalance += total[i].balance;
}
return totalBalance;
}
return (
<ScrollView style={styles.scrollView}>
<View style={styles.container}>
<Header />
<View style={styles.greetingsContainer}>
<Text style={styles.greetings}>
Welcome back,<Text style={styles.userName}> Natalie Tang!</Text>
</Text>
</View>
<BalanceCard
balance={totalBalance()}
spent={data.spent}
points={data.points}
/>
<QuickActionsSection />
<OnloadRecentTransactionsAPI />
</View>
</ScrollView>
);
}
当您无法将值存储到状态时,会出现什么错误响应?@FathurifkiElvariantoGamal此错误:类型为“Promise”的参数不能分配给类型为“SetStateActionimport React,{useState}的参数(来自“React”),是否执行了此操作?@FathurifkiElvariantoGamal是的是的,我执行了。我已经编辑了代码。请尝试使用以下常量[accountDetails,setAccountDetails]=useState([]如有);
accounts: t.array(
t.type({
username: t.string,
accountNumber: t.string,
accountType: t.string,
productName: t.string,
balance: t.number,
}),
),
transactionsList: t.array(
t.type({
userId: t.string,
type: t.string,
currentAccountNumber: t.string,
accountNumber: t.string,
amount: t.number,
notes: t.string,
transactionTime:t.string,
}),
),```