So, I worked on this for some time and thought it was best to reorganize my code so that the state was configured as an array of objects. What I'm trying to do is increase the number with the click of a button.
I have a callback function in a component that runs a function to update the state ... however, I am having difficulty targeting a key value inside an object.
My initial state is as follows:
getInitialState: function() { return { items: [ { links: 'zest', trackId: 1023, songTitle: 'z know the others', artist: 'zuvet', upVotes: 0 }, { links: 'alpha', trackId: 987, songTitle: 'ass', artist: 'arme', upVotes: 3 }, ] }
I am trying to target upVotes , but cannot figure out how to do this. My function passes the key so that I can target the index in the array, but when I try to do something like: this.setState({items[key]: {upVotes: this.state.items[key].upVotes + 1}}) , it gives an error due to an unexpected token [ .
I tried something similar to this thread here , but I keep getting errors.
What function can I write that will only setState for the key in the object I want to configure?
source share