Reputation: 5
import React from 'react';
import Button from '@material-ui/core/Button';
const values=['change','me']
export default class Mycomp extends React.Component{
constructor(props){
super(props)
this.state={
'change': 0,
'me': 0,
}
}
clickhand(item,n){
this.setState({item:n})//it assigns the given value to n but not to item so how do you loop ?
};
render(){
return(
values.map((element)=>{return(
<Button variant="contained" size="small" color="primary" style={{margin:'3px'}}
onClick={()=>this.clickhand(element,(this.state[element]+1))}>
+{this.state[element]}
</Button>)})
)}}
so how can i change the values in the state with a loop ?
Upvotes: 0
Views: 31
Reputation: 352
this.setState({item:n})
item
here is considered as property name but not a variable which value should be used as property name.
try to change it to
this.setState({ [item] : n })
Upvotes: 1