使用地图功能时,单选按钮选择问题

时间:2019-06-11 05:23:35

标签: javascript reactjs material-ui react-material

我有一个数组列表,我正在使用map函数进行渲染。每个对象都有一个单选按钮。现在,当我单击数据的单选按钮时,它显示“ TypeError:无法读取未定义的属性'value'”。我不明白,我做错了。请在下面检查我的代码。

从'react'导入React,{组件} 从'@ material-ui / core'导入{withStyles,Typography,Grid,Radio};

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)

3 个答案:

答案 0 :(得分:1)

用于处理更改的函数应该在以下事件中发生:handleChange(e) --> selectedValue: e.target.value,但是在onChange属性中,您传递的是ID:onChange = {()=>this.handleChange(list.id)}

只需更改您的handleChange函数以将id用作参数并使用它来设置状态:handleChange(id) ---> selectedValue: id

答案 1 :(得分:0)

<Radio
    ...
    onChange = {(e)=>this.handleChange(e)}
or
    onChange = {this.handleChange}
    ...
/>

e.target存在于点击事件中。

答案 2 :(得分:0)

handleChange=(id)=>{
    this.setState({
        selectedValue: id
    })
}