Reputation: 271
I am having an list of array, which i am rendering the using the map function. Each object will have an radio button. Now, when i am clicking the radio button of a data, it shows, "TypeError: Cannot read property 'value' of undefined". I don't get, what i am doing wrong. Please check my code below.
import React, { Component } from 'react' import { withStyles, Typography, Grid, Radio } from '@material-ui/core';
class EventSupervisor extends Component {
constructor(props){
super(props);
this.state = {
lists: [
{id: 1, proImg: require("../../../../../assets/images/man-avatar.jpg"), imgAlt: 'Profile Image', name: 'Sam', role: 'Teacher'},
{id: 2, proImg: require("../../../../../assets/images/man-avatar.jpg"), imgAlt: 'Profile Image', name: 'Sam', role: 'Teacher'},
{id: 3, proImg: require("../../../../../assets/images/man-avatar.jpg"), imgAlt: 'Profile Image', name: 'Sam', role: 'Teacher'}
],
selectedValue: 1
}
}
handleChange=(e)=>{
this.setState({
selectedValue: e.target.value
})
}
render() {
const { classes } = this.props
return (
<div className= {classes.supervisorWrapper}>
<Grid container>
<Grid item xs='6'>
<Typography className={classes.selectContent}>Select Supervisor</Typography>
{/* <SupervisorList lists={this.state.lists} selectedValue={this.state.selectedValue} onChange={this.handleChange}/> */}
{
this.state.lists.map((list)=> {
return (
<div className={classes.superWrapper} key={list.id}>
<img src={list.proImg} alt={list.imgAlt} />
<Typography className={classes.supervisorName}>{list.name}<span className={classes.supervisorRole}>{list.role}</span></Typography>
<Radio
checked = {this.state.selectedValue === list.id}
onChange = {()=>this.handleChange(list.id)}
value = {this.state.selectedValue}
classes={{
root: classes.radioRoot,
checked: classes.rootChecked
}}
/>
</div>
)
})
}
</Grid>
<Grid item xs='6'>
</Grid>
</Grid>
</div>
)
}
}
export default withStyles(styles)(EventSupervisor)
Upvotes: 0
Views: 5915
Reputation: 9063
The function for handling the change is expected the event: handleChange(e) --> selectedValue: e.target.value
, but in your onChange prop you're passing in the id: onChange = {()=>this.handleChange(list.id)}
Just change you handleChange
function to take the id as an argument and use that to set the state: handleChange(id) ---> selectedValue: id
Upvotes: 2
Reputation: 1057
<Radio
...
onChange = {(e)=>this.handleChange(e)}
or
onChange = {this.handleChange}
...
/>
e.target exists in click event.
Upvotes: 0