所以我的代码是:更新状态
export default class MyClass extends Component {
constructor(props) {
super(props);
this.state = {
data: [
{id: 101, name:"One", thevalue:11},
{id: 102, name:"Two", thevalue:22},
{id: 103, name:"three", thevalue:33}
]
}
}
handleOnPress() {
<< HOW DO I CODE THIS ?? >>
I want to increase the number count in thevalue of the pressed item
}
render() {
return(
<FlatList
data = {this.state.data}
renderItem = {
({item}) =>
<TouchableOpacity onPress={this.handleOnPress} >
<Text> {item.name} + {item.thevalue} </Text>
</TouchableOpacity>
}
/>
)
}
}
我希望能够增加只有thevalue
单击的项目的数量。所以我应该做一个setState
对不对?但是我怎么知道我需要运行哪个项目呢?我是否需要将点击项目的id
传递给函数?如果是的话,我该怎么做?
非常感谢。
UPDATE1:
handleOnPress(id) {
this.setState({
thevalue: this.state.thevalue+1
});
}
你能提供'item'在'onPress'?例如:'{this.handleOnPress(item)}''或类似的? –
@JeffHuijsmans你不能这样称呼它,或者handleOnPress将在每个物品的渲染上被调用。你需要按照我的回答做些事情:) –
@MattDerrick很好,谢谢!我想在另一个框架的方式:) –