Reactjs props.deleteComments不是函数
我在遵循指南中的一些代码,我发现了这个错误 在我的App.js中,我有:Reactjs props.deleteComments不是函数,reactjs,ruby-on-rails-5,Reactjs,Ruby On Rails 5,我在遵循指南中的一些代码,我发现了这个错误 在我的App.js中,我有: deleteComments = async (commentId) => { try { await axios.delete(`/comments/${commentId}`); const comments = await this.getComments(); this.setState({ comments }); } catch (error) { console.log(error);
deleteComments = async (commentId) => {
try {
await axios.delete(`/comments/${commentId}`);
const comments = await this.getComments();
this.setState({ comments });
} catch (error) {
console.log(error);
}
};
注释列表组件
const CommentsList = (props) => {
const comments = props.comments.map((comment) => {
return (
<Comment
{...comment}
deleteComments={props.deleteComments}
key={comment.id}
/>
);
});
const CommentsList=(道具)=>{
const comments=props.comments.map((comment)=>{
返回(
);
});
我在其中调用函数的Comment组件
import React from 'react';
const Comment = (props) => {
const deleteComments = () => {
props.deleteComments(props.id);
};
return (
<div>
...
<div>
<button onClick={deleteComments}>Delete</button>
</div>
</div>
);
};
从“React”导入React;
常量注释=(道具)=>{
常量deleteComments=()=>{
props.deleteComments(props.id);
};
返回(
...
删除
);
};
很明显,CommentList
没有通过deleteComments
prop。您需要通过它:
<CommentList deleteComments={deleteComments} />
CommentList没有通过deleteComments
prop。演示如何使用它。该prop不是deleteComments={props.deleteComments}?但是deleteComments
如何进入props?您是否将其传递到CommentList
?哦,好的,我知道哪里出了问题,并修复了它,现在它正在工作。谢谢:)