Reactjs React native不在useState()之后更新UI
我试图用Reactjs React native不在useState()之后更新UI,reactjs,react-native,fetch,nested-lists,use-state,Reactjs,React Native,Fetch,Nested Lists,Use State,我试图用express呈现react native中的嵌套列表 最初,我使用onPress事件用一级列表更新状态,我试图通过将特定的子列表对象嵌套到appropirate列表对象中来更新状态 这是我的密码 <TouchableWithoutFeedback onPress={ async () => { fetch(`http://192.168.64.1:3000/getTypes/${item.id}`, {
express
呈现react native
中的嵌套列表
最初,我使用onPress
事件用一级列表更新状态,我试图通过将特定的子列表
对象嵌套到appropirate列表对象
中来更新状态
这是我的密码
<TouchableWithoutFeedback
onPress={ async () => {
fetch(`http://192.168.64.1:3000/getTypes/${item.id}`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
})
.then((response) => response.json())
.then((data) => {
let changedState = [...retrivedData]
changedState.map((dataItem) => {
if(dataItem.id == item.id){
dataItem.checked = !dataItem.checked
dataItem.subCategory = data
}
})
setRetrivedData(changedState);
console.warn(retrivedData);
})
}} >
<View key={item.id}>
<View style={styles.menus} key={item.id}>
<Text style={[{ fontWeight: 'normal', color: 'black' }, styles.menuTitle]}>{item.name}</Text>
<Icon style={styles.icon} name={item.checked ? 'chevron-small-up' : 'chevron-small-down' } type='entypo' />
</View>
{item.checked &&
retrivedData.map((category) => {
if(category.categoriesId == item.id){
if(category.subCategory.length > 0)
{
category.subCategory.map((subItem) => {
return(
<View style={styles.subMenus}>
<Text style={styles.subMenu}>{subItem.name}</Text>
</View>
)
})
}
}
})
}
</View>
</TouchableWithoutFeedback>
这是我更新后的代码
[
{"checked": true,
"id": 1,
"name": "clothing",
"productCategoryId": 1,
"subCategory": [
[Object],
[Object],
[Object],
[Object],
[Object],
[Object]
]
},
{
"checked": false,
"id": 2,
"name": "footwear",
"productCategoryId": 1
},
...
]
要更新UI,您必须使用
this.setState()
方法更新状态,该方法用于您尝试将数据置于该状态,这将自动重新启动UI我使用的是嵌套映射方法,因此它会在映射方法完成其过程之前设置状态
我通过在安装过程中获取所有信息来解决这一问题。。
谢谢,请告诉我更多关于你想做什么的信息,因为如果你试图循环数据,你可以使用
,我可以告诉你我是如何使用函数组件的,所以我使用的是useState钩子,…我不知道你可以把它变成类组件,然后使用setState()
会更简单
[
{"checked": true,
"id": 1,
"name": "clothing",
"productCategoryId": 1,
"subCategory": [
[Object],
[Object],
[Object],
[Object],
[Object],
[Object]
]
},
{
"checked": false,
"id": 2,
"name": "footwear",
"productCategoryId": 1
},
...
]