Reactjs 调用表中的函数
我正在创建一个表,其中包含一个从数组中删除值的按钮,以调用api删除选定值,但单击该按钮会导致错误 未捕获的TypeError:无法读取未定义的react dom.production.min.js:52的属性“doDelete” 这是构造函数Reactjs 调用表中的函数,reactjs,function,button,constructor,onclick,Reactjs,Function,Button,Constructor,Onclick,我正在创建一个表,其中包含一个从数组中删除值的按钮,以调用api删除选定值,但单击该按钮会导致错误 未捕获的TypeError:无法读取未定义的react dom.production.min.js:52的属性“doDelete” 这是构造函数 constructor(props) { super(props); this.state = { diary:[], isLoaded: false, diaryid:''
constructor(props)
{
super(props);
this.state =
{
diary:[],
isLoaded: false,
diaryid:''
}
this.doDelete = this.doDelete.bind(this);
}
下面是一些代码,这似乎就是问题所在
doDelete(diaryID)
{
console.log("In Function")
}
renderPerson(diary, index) {
return (
<tr key={index}>
<td>{diary.user}</td>
<td>{diary.note}</td>
<td>{diary.date}</td>
<td>
<button onClick={() => { this.doDelete(diary._id) }} className="delete-btn">Delete</button>
</td>
</tr>
)
}
render() {
var {isLoaded, diary} = this.state;
if(Userstore.loading)
{
return (
<div className="app">
<div className='container'>Loading, please wait...</div>
</div>
)
}
else
{
if(Userstore.isLoggedIn)
{
return (
<div className="app">
<div className='container'>Hello {Userstore.username}
<SubmitButton
text={'Logout'}
disabled={false}
onClick={ () => this.doLogout() }
/>
<ReactBootStrap.Table striped bordered hover>
<thead>
<tr>
<th>Username</th>
<th>Note</th>
<th>Date</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{diary.map(this.renderPerson)}
</tbody>
</ReactBootStrap.Table>
</div>
</div>
)
}
return (
<div className="app">
<div className='container'>
<LoginForm />
</div>
</div>
);
}
}
dodelite(diaryID)
{
console.log(“登录功能”)
}
渲染人员(日志、索引){
返回(
{diary.user}
{日记.笔记}
{日记日期}
{this.doDelete(diary.\u id)}className=“delete btn”>删除
)
}
render(){
var{isLoaded,diary}=this.state;
if(Userstore.loading)
{
返回(
正在加载,请稍候。。。
)
}
其他的
{
if(Userstore.isLoggedIn)
{
返回(
你好{Userstore.username}
this.doLogout()}
/>
用户名
注
日期
删除
{diary.map(this.renderPerson)}
)
}
返回(
);
}
}
这是否回答了您的问题?您需要绑定传递给其他组件的函数。在你的情况下,那将是renderPerson
而不是doDelete
是的,那是永远不会成功的。谢谢你,太多了,加布里埃尔