- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 React Native 的新手,这是我第一个使用 React Native 从头开始构建的项目。我有一个警告问题,我想要的功能不起作用。
以下是我想要的一些解释:
我有一个对象数组,我将其命名为购物车,在购物车内它看起来像:
let carts = [
{
merchantId: 1,
items: [
{
productId: 1,
productQty: 1,
},
{
productId: 2,
productQty: 5,
},
],
},
{
merchantId: 2,
items: [
{
productId: 3,
productQty: 2,
},
{
productId: 4,
productQty: 4,
},
],
},
];
这是我的屏幕:
import React, { useState, useEffect } from 'react';
import {
StyleSheet,
Text,
View,
Image,
ScrollView,
TouchableOpacity,
} from 'react-native';
import { Button } from 'react-native-paper';
import { theme } from '../../../infrastructure/theme';
import SpacerComponent from '../../../components/SpacerComponent';
import { FontAwesome5 } from '@expo/vector-icons';
import MerchantCardComponent from '../components/MerchantCardComponent';
import { tranformCartArray } from '../../../services/carts/CartTransform';
const CartScreen = ({ navigation }) => {
const [carts, setCarts] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const getCarts = async () => {
setIsLoading(true);
const response = await tranformCartArray();
setCarts(response);
setIsLoading(false);
};
useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
getCarts();
});
return unsubscribe;
}, [navigation]);
console.log(carts);
if (carts.length === 0) {
return (
<View style={styles.emptyContainter}>
<Image
style={styles.imageCart}
source={require('../../../assets/images/empty_cart.png')}
/>
<Text style={styles.emptyCartText}>
Keranjangnya masih kosong nih !
</Text>
<SpacerComponent size="xlarge" />
<Button
style={styles.buttonExplore}
mode="contained"
size={20}
onPress={() => navigation.navigate('Merchants')}
>
<FontAwesome5
name="shopping-cart"
size={20}
color={theme.colors.text.inverse}
/>
<Text>{' '}</Text>
<Text style={styles.buttonText}>Eksplor</Text>
</Button>
</View>
);
}
if (isLoading) {
return (
<View style={styles.emptyContainter}>
<ActivityIndicator
color={theme.colors.ui.primary}
size={theme.sizes[3]}
/>
</View>
);
}
return (
<ScrollView style={styles.container}>
{carts.map((merchantAndItems) => {
return (
<TouchableOpacity onPress={() => navigation.navigate('Checkout')}>
<MerchantCardComponent
merchantAndItems={merchantAndItems}
key={merchantAndItems.merchantId}
/>
</TouchableOpacity>
);
})}
</ScrollView>
);
};
export default CartScreen;
const styles = StyleSheet.create({
emptyContainter: {
justifyContent: 'center',
alignItems: 'center',
flex: 1,
backgroundColor: theme.colors.bg.primary,
},
imageCart: {
maxWidth: '90%',
maxHeight: '45%',
aspectRatio: 1,
},
emptyCartText: {
fontSize: 20,
fontFamily: theme.fonts.regular,
},
buttonExplore: {
backgroundColor: theme.colors.ui.primary,
},
buttonText: {
fontSize: 20,
},
container: {
flex: 1,
marginBottom: 10,
},
});
我想在将数组的购物车转换为 MerchantCardComponent 后传递,我想要的转换类型是数组(购物车)变为:
let carts = [
{
merchantId: 1,
merchantName: 'Merchant A',
merchantLogo: 'https://assets.backend.com/merchants/1/logo_url.jpg',
items: [
{
productId: 1,
productName: 'Product One',
productImage: 'https://assets.backend.com/merchants/1/products/1/product_image.jpg',
productPrice: 2000,
productQty: 1,
},
{
productId: 2,
productName: 'Product Two',
productImage: 'https://assets.backend.com/merchants/1/products/2/product_image.jpg',
productPrice: 2000,
productQty: 5,
},
],
},
{
merchantId: 2,
merchantName: 'Merchant B',
merchantLogo: 'https://assets.backend.com/merchants/2/logo_url.jpg',
items: [
{
productId: 3,
productName: 'Product Three',
productImage: 'https://assets.backend.com/merchants/2/products/3/product_image.jpg',
productPrice: 2000,
productQty: 2,
},
{
productId: 4,
productName: 'Product Four',
productImage: 'https://assets.backend.com/merchants/4/products/4/product_image.jpg',
productPrice: 2000,
productQty: 4,
},
],
},
];
这样我就可以轻松地打印出文本,而无需在屏幕上再次获取后端。我已经为转换购物车功能制作了一个单独的文件,这里是代码:
import React, { useContext, useState } from 'react';
import CartsContext from './CartsContext';
import {
doRequestMerchantDetail,
doRequestProductDetail,
} from '../merchants/MerchantsService';
export const tranformCartArray = async () => {
const [transformCart, setTransformCart] = useState([]);
const [afterTransform, setAfterTransform] = useState(null);
const [items, setItems] = useState([]);
const { carts } = useContext(CartsContext);
for (let i = 0; i < carts.length; i++) {
let [errMerchant, merchant] = await doRequestMerchantDetail(
carts[i].merchantId,
);
for (let j = 0; j < cart[i].items.length; j++) {
let [errProduct, product] = await doRequestProductDetail(
carts[i].items[j].productId,
);
if (product) {
setItems(
...items,
...[
{
productName: product.product_name,
productQty: carts[i].items[j].productQty,
productPrice: product.product_price,
productId: carts[i].items[j].productId,
productImage: product.product_image_url,
},
],
);
} else {
setItems(
...items,
...[
{
productName: 'Error',
productQty: carts[i].items[j].productQty,
productPrice: 'Error',
productId: carts[i].items[j].productId,
productImage:
'https://cdn.pixabay.com/photo/2021/07/21/12/49/error-6482984_960_720.png',
},
],
);
}
}
if (merchant) {
setAfterTransform({
merchantName: merchant.name,
merchantLogo: merchant.merchant_logo_url,
merchantId: carts[i].merchantId,
items,
});
} else {
setAfterTransform({
merchantName: 'Error',
merchantLogo: 'Error',
merchantId: carts[i].merchantId,
items,
});
}
setTransformCart(...transformCart, ...[afterTransform]);
}
return transformCart;
};
直到现在我仍然混淆是什么让警告显示出来,我在第一句话中提到的不工作是它在转换后返回一个空数组。这是控制台的屏幕截图
最佳答案
tranformCartArray
不是 react 组件或自定义 react 钩子(Hook),所以你不能使用 useState
和 useContext
钩子(Hook)。
此外,抛出“未处理的拒绝”是因为 getCarts
函数声明 async
所以它隐式返回一个 Promise 并且当 React hooks 错误被抛出时没有 catch
block 或 .catch
一个 Promise 链来捕获和处理它。
我建议通过 carts
从上下文状态进入tranformCartArray
实用程序函数,并返回一个增强的购物车数组。这允许您从函数中删除任何和所有钩子(Hook)。
export const tranformCartArray = async (carts) => {
const newCarts = [];
for (let i = 0; i < carts.length; i++) {
let [, merchant] = await doRequestMerchantDetail(
carts[i].merchantId,
);
const newItems = [];
for (let j = 0; j < carts[i].items.length; j++) {
let [, product] = await doRequestProductDetail(
carts[i].items[j].productId,
);
newItems.push({
productName: product?.product_name ?? 'Error',
productQty: carts[i].items[j].productQty,
productPrice: product?.product_price ?? 'Error',
productId: carts[i].items[j].productId,
productImage: carts[i].items[j].product_image_url,
});
}
newCarts.push({
merchantName: merchant?.name ?? 'Error',
merchantLogo: merchant?.merchant_logo_url ?? 'Error',
merchantId: carts[i].merchantId,
items: newItems,
});
}
return newCarts;
};
购物车屏幕
const CartScreen = ({ navigation }) => {
const [carts, setCarts] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const { carts: contextCarts } = useContext(CartsContext); // <-- access context
const getCarts = async () => {
setIsLoading(true);
try {
const response = await tranformCartArray(contextCarts); // <-- pass context carts value
setCarts(response);
} catch(error) {
// handle any errors?
} finally {
setIsLoading(false);
}
};
...
关于javascript - React Native : [Unhandled promise rejection: Error: Invalid hook call. Hooks 只能在函数组件的主体内部调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68697987/
我一直在努力理解下面两个的区别,哪个是空闲的使用方式: let getClient = () => { return connect() .then((client) => {
我正在学习一门关于 JavaScript 函数式编程的很棒的在线类(class)。在讲师使用 Array.prototype.reject() 并且它在运行时对我不起作用之前,我一直很好。 我想使用“
这个问题在这里已经有了答案: Do I need to return after early resolve/reject? (6 个答案) 关闭 6 年前。 我是 Promise 的新手,我想知道
我对 Angular JS deferred 和 $q 感到困惑。我找到了这个 SO Question这解释了 $q.defer() 和 $q 之间的区别。它解释了 $q.reject is a sh
我正在尝试处理 Angular $q 服务及其相关对象和 API。当我查看控制台中的对象时,我看到: var deferred = $q.defer() ...(and then from conso
我在运行测试时遇到问题(在 Node 中), 我正在模拟一个被拒绝的 promise ,我的代码应该重试(使用 promise-retry 如果可能相关的话)。 当我使用 stub.returns(P
当我编译或运行它时,程序会显示正确的信息。问题是当我检查程序时。它显示“:(拒绝”“分钟检查我的时等待输入被拒绝时超时”程序。我也尝试使用 GetInt 和 get_int 。你能帮助我吗,请? in
对于 Ruby 中的 Hash,reject! 和 reject 与 delete_if 有何不同?谁能用简单的代码片段解释它们之间的区别? 最佳答案 由于其他答案指的是 Array#delete_i
当我尝试使用 Firestore 获取数据时,出现上述错误 - 我正在尝试从数据库检索 token ,以便可以发送消息: exports.getUsers = functions.https.onRe
这个问题在这里已经有了答案: Are JavaScript forever-pending promises bad? (2 个答案) 关闭 4 年前。 问题是这样的 function demo()
您好,我正在尝试调用返回 promise 的异步函数 makeRemoteExecutableSchema。 async function run() { const schema = await
我发出的每个 http 请求似乎都会出现此错误。我不太确定它来自哪里? (node:39390) UnhandledPromiseRejectionWarning:未处理的 promise 拒绝(拒绝
我的代码运行良好,但今天运行时发生了这种情况: (node:8592) UnhandledPromiseRejectionWarning: Unhandled promise rejection (r
我正在尝试从 mlabs 连接 mongodb。我插入了以下代码: Mongoose.connect('mongodb://:@ds163402.mlab.com:63402/projecttwist
我已将 Cucumber 与 nightwatch.js 集成。 我的 package.json 看起来像:- { "name": "learning-nightwatch", "versio
目前,我在“Javascript”代码中遇到了 promise 问题。它不断抛出“TypeError: res.status(...).json(...).catch is not a functio
首先,请看这个demo。 function loadImageAsync(url) { return new Promise(function(resolve, reject) { var
我已经在这个问题上工作了很长时间,但我无法真正解决它。当我执行 ionic Cordova build android 时,它运行良好,直到它到达 Cordova build android,任何人都
不知道是什么导致了这个问题。昨天它运行良好。今天,当我尝试运行 react-native run-android 时。我收到这个错误。有任何想法吗? Starting JS server... Run
我是第一次使用.then,而不是.then我使用回调函数。 下面是我的代码片段: phantom.create().then(function (ph) { ph.createPage().t
我是一名优秀的程序员,十分优秀!