Javascript 将参数传递给React中的映射回调函数时出错
我有一个React组件,它使用map函数来显示一些数据。问题是,当我将回调函数传递给并使用classes属性时,我得到一个错误 未捕获引用错误:未定义类 这是我的密码。请注意,类是定义的。如何在我拥有的回调函数上正确访问它Javascript 将参数传递给React中的映射回调函数时出错,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,我有一个React组件,它使用map函数来显示一些数据。问题是,当我将回调函数传递给并使用classes属性时,我得到一个错误 未捕获引用错误:未定义类 这是我的密码。请注意,类是定义的。如何在我拥有的回调函数上正确访问它 class ProvidersPage extends React.Component { constructor(props, context) { super(props, context); } providerRow(provider, inde
class ProvidersPage extends React.Component {
constructor(props, context) {
super(props, context);
}
providerRow(provider, index) {
return <TableRow className={classes.row} key={index}>
<CustomTableCell component="th" scope="row">
{provider.name}
</CustomTableCell>
<CustomTableCell>{provider.type}</CustomTableCell>
<CustomTableCell>{provider.pointOfContact}</CustomTableCell>
<CustomTableCell>{provider.telephoneNumber}</CustomTableCell>
<CustomTableCell>{provider.licenseNumber}</CustomTableCell>
<CustomTableCell>{provider.licenseSource}</CustomTableCell>
<CustomTableCell>
<IconButton>
<MoreHorizIcon />
</IconButton>
</CustomTableCell>
</TableRow>
}
render() {
const { classes } = this.props;
return (
<div>
<Paper className={classes.root} elevation={4}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<CustomTableCell>Name</CustomTableCell>
<CustomTableCell>Type</CustomTableCell>
<CustomTableCell>Point of Contact</CustomTableCell>
<CustomTableCell>Telephone Number</CustomTableCell>
<CustomTableCell>License Number</CustomTableCell>
<CustomTableCell>License Source</CustomTableCell>
<CustomTableCell>Actions</CustomTableCell>
</TableRow>
</TableHead>
<TableBody>
{this.props.providers.map(this.providerRow)}
</TableBody>
</Table>
</Paper>
</div>
);
}
}
class ProvidersPage扩展了React.Component{
构造函数(道具、上下文){
超级(道具、背景);
}
providerRow(提供程序,索引){
返回
{provider.name}
{provider.type}
{provider.pointOfContact}
{provider.telephoneNumber}
{provider.licenseNumber}
{provider.licenseSource}
}
render(){
const{classes}=this.props;
返回(
名称
类型
接触点
电话号码
许可证号码
许可证来源
行动
{this.props.providers.map(this.providerRow)}
);
}
}
必须将classes变量传递给回调函数才能将其作为参数传递
this.props.providers.map(this.providerRow.bind(this, classes))
providerRow功能原型为:
providerRow(classes, provider, index)
或者你的道具中也有类,所以你必须写这一行来开始providerRow函数:
const { classes } = this.props;
更改providerRow:
className={this.props.classes.row}
@nirsky如果他/她这样做,他/她还需要映射(this.providerRow,this)
。您好@nirsky,但是现在显示的错误是uncaughttypeerror:无法读取未定义的属性“props”。添加到构造函数:this.providerRow=this.providerRow.bind(this)
@ASDFGerte谢谢。成功了。在这类问题中,你们有并没有建议某人熟悉这个关键词的文章?