0

So my code is :

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>
            }
        />
    )
  }
}

I want to be able to increase the count of the thevalue of only the item clicked. So I should do a setState right? But how do I know for which item I need to run that on? Do I need to pass the id of the item clicked to the function? If yes, how do I do that?

Many thanks.

UPDATE1:

handleOnPress(id) {
      this.setState({
        thevalue: this.state.thevalue+1
    });
}
Somename
  • 3,376
  • 16
  • 42
  • 84

2 Answers2

3

You have to give it an argument so we know what item is to increment:

onPress={this.handleOnPress.bind(this, item.id)}
...
handleOnPress(id) {
    // increment id
}

or this is a little bit more readable but doing the same thing:

onPress={() => this.handleOnPress(item.id)}
Matt Derrick
  • 5,674
  • 2
  • 36
  • 52
  • I have my function: `this.setState({thevalue: this.state.thevalue+1});` but not working. – Somename Aug 16 '17 at 15:19
  • You are literally updating with "thevalue"? You need to update 'data[X].thevalue' where X is position of the object with the same ID. – Matt Derrick Aug 17 '17 at 09:06
  • I did that. Followed https://stackoverflow.com/questions/45717081/update-state-of-only-the-clicked-element/45717227#45717227 but it's not updating the data real time. I have to save something in the page and Hot Reloading will do the trick. I want the data to be updated realtime. – Somename Aug 17 '17 at 14:42
1

You can pass the id to onPress then update the corresponding thevalue

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(id) {
    let {data} = this.state;
    let idx = data.findIndex(x => x.id == id);
    data[idx].thevalue ++;
    this.setState({data});
  }

  render() {
      return(
        <FlatList
            data = {this.state.data}
            renderItem = {
                ({item}) => 
                <TouchableOpacity onPress={() => this.handleOnPress(item.id)} >
                    <Text> {item.name} + {item.thevalue} </Text>
                </TouchableOpacity>
            }
        />
    )
  }
}
An Nguyen
  • 1,487
  • 10
  • 21