gpt4 book ai didi

react-native - React Native Flatlist renderItem

转载 作者:行者123 更新时间:2023-12-04 05:23:12 25 4
gpt4 key购买 nike

使用 React Native 时,FlatList 组件存在一些问题。
这是我的 FlatList

    <FlatList
data={this.state._data}
renderItem={() => this.renderItem()}
refreshControl={
<RefreshControl
onRefresh={() => this.handleRefresh}
refreshing={this.state.refreshing}
/>
}
/>

这是我的 renderItem 函数:
    renderItem({item, index}) {
return (
<View style={{marginTop: 10, marginHorizontal: 10, paddingLeft:
10}}>
<ListItem
roundAvatar
title={`${item.itemName}`}
subtitle={`${item.amount}`}
avatar={require('../../../images/logo.png')}
/>
<View
style={{
paddingBottom: 10,
paddingTop: 10,
display: 'flex',
flexDirection: "row",
justifyContent: "space-around",
alignContent: "center"
}}
>
<View style={{ flexDirection: "row", alignContent:
"center", width:"45%"}}>
<Button
block
small
// disabled={this.state.acceptButtonGray}
style=
{this.state.acceptButtonGray ? ({
backgroundColor: 'gray',
width: "100%"
}) : ({backgroundColor: "#369ecd",
width: "100%"
})}
onPress={() =>
this.setState({
modalVisible: true,
// acceptOrDeclineModalText: `Accept offer for ${item.amount} ${'\b'} Are you Sure?`,
acceptOffer: true,
})
}
>
<Text>
Accept
</Text>
</Button>
</View>
</View>
</View>
);
}

按钮中 onPress 中的 this.setState 应该使 Modal 可见,并将 acceptOffer 设置为 true。模态打开,用户确认他们的报价。现在打开该模式的报价按钮应该是灰色的,甚至更好的是禁用。

如上所示传递我的 RenderItem 函数,我收到
    TypeError: Cannot read property 'item' of undefined.

像这样传递我的 RenderItem 函数:
    renderItem={this.renderItem}

我收到此错误:
    _this2.setState is not a function

FlatList 组件当然要对我的部分问题负责,以及我调用 this.setState 的方式和位置。我的帖子里只显示了一个按钮,但是有两个,一个是接受,一个是拒绝。有两个模态会改变什么吗?

FlatList 可以轻松地显示我的 ListItem 组件,直到我尝试在包含这些 ListItem 的 View 内的按钮中调用 this.setState。

模态关闭按钮接受 this.state.acceptOffer,如果为真,则将 this.state.acceptButtonGray 设置为真,这个逻辑应该在其他地方吗?

是否有另一种方法可以在不使用组件状态的情况下打开模态并更改按钮颜色? react 是否需要将这些按钮放在 TouchableOpacity 中?

我非常感谢您提供的任何帮助。

最佳答案

你应该写一个这样的renderItem函数

renderItem = ({item, index}) => {
// return here
}

关于react-native - React Native Flatlist renderItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47934816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com