Javascript 使用react native基于listView中的rowid递增和递减值
我正在运行一个示例项目。在我的项目中,我使用的是listView。如果单击向上箭头按钮,则该值将增大,单击向下箭头按钮,则该值将减小。当我单击这两个按钮中的任何一个时,listView中所有行中的值都将更改。如何在listView中仅更改特定行中的值。请给我任何建议 这是我的密码:Javascript 使用react native基于listView中的rowid递增和递减值,javascript,react-native,Javascript,React Native,我正在运行一个示例项目。在我的项目中,我使用的是listView。如果单击向上箭头按钮,则该值将增大,单击向下箭头按钮,则该值将减小。当我单击这两个按钮中的任何一个时,listView中所有行中的值都将更改。如何在listView中仅更改特定行中的值。请给我任何建议 这是我的密码: class Example extends Component { constructor(props){ super(props); this.state={ dataS
class Example extends Component {
constructor(props){
super(props);
this.state={
dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
count:1,
}
}
incrementFunc(rowID:number, listItems){
this.setState({
count : this.state.count + 1
},()=>{
this.setState({
dataSource:new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(array)
})
})
}
decrementFunc(rowID:number,listItems){
this.setState({
count : this.state.count- 1
},()=>{
if(this.state.count <= 1){
this.setState({
count : 1
})
}
},()=>{
this.setState({
dataSource:new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(array)
})
})
}
listOfItems(listItems, sectionID:number, rowID:number){
return(
<View style = {styles.buttonContainer}>
<View style={styles.arrowsView}>
<TouchableOpacity onPress ={this.incrementFunc.bind(this, rowID, listItems )}>
<Image style={styles.arrowStyle} source={require('@images/up-arrow.png')} />
</TouchableOpacity>
</View>
<View style={styles.numberView}>
<Text style={{fontSize:15, color:'black'}}>{this.state.count}
</Text>
</View>
<View style={styles.arrowsView}>
<TouchableOpacity onPress ={this.decrementFunc.bind(this, rowID, listItems)}>
<Image style={styles.arrowStyle} source={require('@images/down-arrow.png')} />
</TouchableOpacity>
</View>
</View>
)
}
render(){
return(
<View style={styles.listview}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.listOfItems.bind(this)}/>
</View>
);
}
}
类示例扩展组件{
建造师(道具){
超级(道具);
这个州={
dataSource:new ListView.dataSource({rowHasChanged:(r1,r2)=>r1!==r2}),
计数:1,
}
}
incrementFunc(rowID:number,listItems){
这是我的国家({
计数:this.state.count+1
},()=>{
这是我的国家({
dataSource:new ListView.dataSource({rowHasChanged:(r1,r2)=>r1!==r2}).cloneWithRows(数组)
})
})
}
decrementFunc(rowID:number,listItems){
这是我的国家({
计数:this.state.count-1
},()=>{
如果(this.state.count){
这是我的国家({
dataSource:new ListView.dataSource({rowHasChanged:(r1,r2)=>r1!==r2}).cloneWithRows(数组)
})
})
}
列表项(列表项,节ID:number,行ID:number){
返回(
{this.state.count}
)
}
render(){
返回(
);
}
}
这是我的截图:<> >代码>计数>代码>状态变量,因为所有的行都是相同的,因为它是一个整数,请考虑保持一个状态变量,它是一个看起来像这样的对象:代码> {RoWID1:Curt1,RoWID2,CONTT2} <代码> > EG:{1:15,2:10,y} /代码> < /P> 因为回调将
rowId
作为参数发送
<TouchableOpacity onPress ={this.incrementFunc.bind(this, rowID, listItems )}>
decrementFunc可以用类似的方式进行修改
然后在listOfItems
方法中
<Text style={{fontSize:15, color:'black'}}>
{this.state.count[rowId]}
</Text>
{this.state.count[rowId]}
您是否尝试更改数组中的特定字段,而不是总计数?如果您使用rowHasChanged函数,它必须仅在发生更改时检测更改。因为您对所有项目使用了一种状态…并且您正在减少/inc该状态…因此它会影响所有项目。什么?您能准确地解释此方法吗?我将修改像这样通知我的增量函数.incrementFunc(listItems,rowID:number){let count={…this.state.count}count[rowID]=count[rowID]| | 0;count[rowID]+=1;this.setState({count},()=>{this.setState({数据源:新的ListView.dataSource({rowHasChanged:(r1,r2)=>r1!==r2}).cloneWithRows(数组)}只需将rowId
替换为rowId
。我收到了以下错误:在此环境中,分配的源必须是一个对象。此错误是性能优化,而不是规范complaint@Nandu谢谢你的回复,但我没有得到结果。你能给我看一下演示吗?我不明白你的意思。
<Text style={{fontSize:15, color:'black'}}>
{this.state.count[rowId]}
</Text>