javascript - 在本机反应中设置状态会冻结应用程序
问题描述
在本机反应中,我正在尝试创建一个类组件,该组件加载带有标签和附加 AWS S3 图像的类别列表。
如果您查看此代码,应用程序将冻结在行:
this.setState({ categories });
但是,如果我注释掉该行,控制台会记录该类别数组就好了。
请帮助任何想法为什么?我也尝试过创建一个功能组件,但同样会发生这种情况。
import React, { Component, setState } from "react";
import { View, StyleSheet } from "react-native";
import { isTablet } from "react-native-device-detection";
import { getRecipeCategories } from "../../api/recipeCategoriesApi";
import CategoryImageItem from "./CategoryImageItem";
import { getS3Image, deleteS3Image } from "../../utility/amplify";
class CategoryImagePicker extends Component {
state = {
categories: [],
};
async componentDidMount() {
console.log("loadCats");
let res = await fetch("http://192.168.1.4:4000/api/recipe_categories");
let categories = await res.json();
for (const category of categories) {
const imgUrl = await getS3Image(category.category_image);
category.imageUrl = imgUrl.split("?")[0];
}
console.log("returned");
this.setState({ categories });
console.log("set done...", categories);
}
loadViews() {
let i = 0;
let rowType = "odd";
let viewRows = [];
const { categories } = this.state;
console.log(categories.length);
while (i < categories.length) {
console.log("//", i);
if (isTablet) {
switch (rowType) {
case "odd":
if (i == categories.length - 1) {
viewRows.push(this.renderEvenRow(i, categories[i]));
} else {
viewRows.push(
this.renderOddRow(i, categories[i], categories[i + 1])
);
rowType = "even";
i += 2;
}
break;
case "even":
viewRows.push(this.renderEvenRow(i, categories[i]));
rowType = "odd";
i += 1;
break;
default:
break;
}
} else {
viewRows.push(this.renderEvenRow(i, categories[i]));
}
}
return viewRows;
}
handleShowRecipesForCategory = (category) => {
this.props.onShowRecipesForCategory(category);
};
renderOddRow(i, categoryLeft, categoryRight) {
return (
<View style={styles.oddRow} key={i}>
<CategoryImageItem
category={categoryLeft}
onShowRecipesForCategory={() =>
this.handleShowRecipesForCategory(categoryLeft)
}
/>
<CategoryImageItem
category={categoryRight}
onShowRecipesForCategory={() =>
this.handleShowRecipesForCategory(categoryRight)
}
/>
</View>
);
}
renderEvenRow(i, category) {
return (
<View style={styles.evenRow} key={i}>
<CategoryImageItem
category={category}
onShowRecipesForCategory={() =>
this.handleShowRecipesForCategory(category)
}
/>
</View>
);
}
render() {
return <View style={styles.container}>{this.loadViews()}</View>;
}
}
解决方案
componentDidMount
不应该async
。
componentDidMount(){
this.getCategories()
}
getCategories = async () => {
console.log("loadCats");
let res = await fetch("http://192.168.1.4:4000/api/recipe_categories");
let categories = await res.json();
for (const category of categories) {
const imgUrl = await getS3Image(category.category_image);
category.imageUrl = imgUrl.split("?")[0];
}
console.log("returned");
this.setState({ categories });
console.log("set done...", categories);
}
如果这不起作用,您可以添加您从中获得的console.log("set done...", categories);
内容和您的期望吗
推荐阅读
- java - 验证不适用于 Spring Boot 和 Hibernate
- azure - 将置信度分数添加到 Azure 认知服务 Web 应用
- angular - Angular Universal:错误:找不到“[object Object]”的 NgModule 元数据
- java - PDFTextStripper 提取不正确的泰语文本
- javascript - 使用 GatsbyJS 的 GraphQL 查询中的范围文件夹结构
- sql - 将正确的 SQL 转换为 HQL
- configuration - VuePress 配置中的前端默认值
- c# - 无法在网格控件存储库中添加行lookupedit 在单元格失去焦点时变为空
- powershell - 在 PowerShell ISE 中编辑暂停消息
- mysql - 在mysql中左连接三个表,内连接两个