0

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

导入 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)
4

3 回答 3

2

处理更改的函数预期为 event: handleChange(e) --> selectedValue: e.target.value,但在您的 onChange 道具中,您传递了 id:onChange = {()=>this.handleChange(list.id)}

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

于 2019-06-11T05:26:50.413 回答
2
handleChange=(id)=>{
    this.setState({
        selectedValue: id
    })
}
于 2019-06-11T05:35:35.213 回答
0
<Radio
    ...
    onChange = {(e)=>this.handleChange(e)}
or
    onChange = {this.handleChange}
    ...
/>

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

于 2019-06-11T05:26:13.277 回答