【发布时间】:2019-08-30 13:12:50
【问题描述】:
我有一个包含两个组件的反应应用程序。我的子组件有一个包含一些记录的表。每条记录都有一个按钮,我在 onclick 中调用一个带有参数(id)的函数(editUser)。在那个函数中,我通过 props 将该 id 发送到 Parent 组件。该函数已在构造函数中绑定。但是当按钮点击时,控制台说 this.props.userId is not a function
子组件:
class UsersTable extends React.Component {
constructor(props) {
super(props);
this.state = {
};
this.editUser = this.editUser.bind(this);
}
editUser(id) {
this.props.userId(id);
}
render() {
return (
<TableBody>
{data
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map(n => {
return (
<TableRow key={n.id}>
<TableCell className={classes.fixedTd}>
<BorderColorIcon onClick={() => this.editUser(n.userId)} className="action margin-r" />
</TableCell>
</TableRow>
);
})}
</TableBody>
);
}
}
export UsersTable;
父组件:
class CompanyUserMgt extends Component {
constructor(props) {
super(props);
this.state = {
}
}
editUser = (id) => {
console.log("user", id)
}
render() {
return (
<div className="">
<UsersTable
userId={this.editUser}
key={this.state.tableKey}
/>
</div>
)
}
}
export default CompanyUserMgt;
我该如何解决这个问题?
【问题讨论】:
-
事情对我来说是正确的。我的建议是,尝试记录
this并检查它显示的内容。我的假设是上下文在某处丢失了 -
一切正常。你是如何在
BorderColorIcon中呼叫onClick的? -
您是否确保在类中正确转换箭头函数?您是否尝试以常规方式声明 editUser?
-
@DoğancanArabacı 是的,我也尝试过常规方式。但它不起作用
标签: javascript reactjs