当我控制台记录 this.props 时,我得到一个带有 post.id 的数组,我想使用 onClick 函数从列表中删除此项目,并且不显示在页面上我的代码有什么问题?
所以这就是我所拥有的
export const deletePost = id => dispatch => {
fetch(`https://jsonplaceholder.typicode.com/posts/${id}`, {
method: "DELETE"
})
.then(res => res.json())
.then(post =>
dispatch({
type: DELETE_POST,
payload: id
}),
);
};
my reducer:
const initialState = {
items: [],
item: {}
};
case FETCH_POSTS:
return {
...state,
items: action.payload
};
case DELETE_POST:
return {
...state,
items: action.payload
};
Component contains of
const mapStateToProps = state => ({
posts: state.postsReducer.items,
});
const mapDispatchToProps = {
fetchPosts, deletePost
}
And Delete button on each post:
<button key={post.id} onClick={()=>this.props.deletePost()}>X</button>```