【发布时间】:2020-06-29 06:55:31
【问题描述】:
我正在学习 react-redux,所以现在我正在尝试创建 react-redux crud 应用程序,这是任何解决方案
这里是回购:repo demo
按钮
<span className="delete_info" onClick={() => deleteComment(comment.id) }>Delete</span>
删除元素的动作创建者
export const removeComment = id =>{
return{
type: ActionTypes.DELETE_COMMENTS,
payload:id
}
}
// delete comments
export const deleteComment = id =>{
console.log('ids', id);
return dispatch =>{
dispatch(fetchCommentsRequest())
axios.delete(`/api/v1/todo/${id}`)
.then(response =>{
console.log('yeees mom', response.data)
dispatch(removeComment(id))
})
.catch(error =>{
const erroMsg =error.message;
console.log('eeeror', erroMsg)
dispatch(fetchCommentsFailure(erroMsg))
})
}
}
这是我的减速器
import * as ActionTypes from '../action-types'
const initialState ={
data:[],
error:'',
comments:[],
loading:false,
editing:false
}
const reducer = (state= initialState, action) => {
switch (action.type) {
case ActionTypes.FETCH_COMMENTS_REQUEST:
return{
...state,
loading: true,
}
case ActionTypes.FETCH_COMMENTS_SUCCESS:
return{
...state,
loading:false,
comments:action.payload,
error:''
}
case ActionTypes.FETCH_COMMENTS_FAILURE:
return{
...state,
loading:false,
error:action.payload
}
case ActionTypes.ADD_COMMENTS:
return{
...state,
comments:state.comments.concat(action.payload)
}
case ActionTypes.DELETE_COMMENTS:
return{
...state,
comments: state.comments.filter(comment =>comment.id !==action.payload)
}
case ActionTypes.EDIT_COMMENTS:
return{
...state,
comments: state.comments.map(comment =>comment.id === action.payload?{
...comment,
editing:!editing
}:comment)
}
default: // need this for default case
return state
}
}
export default reducer
现在,当我单击删除时,我会在控制台上看到来自动作创建者的 ID,但该元素并未被删除 并且没有错误,这里有什么问题?
【问题讨论】:
-
如果您在 case ActionTypes.DELETE_COMMENTS 之后执行 console.log(action),并且在 return 之前执行,您会得到什么吗?
-
@tachko 让我检查一下
-
@tachko 按照您的建议添加 console.log(action) 后,我在控制台中什么也看不到
-
请同时添加
removeComment函数的代码 -
看起来你还没有通过
connectHOC 将deleteComment与你的组件连接起来,你只是像普通函数一样调用它。
标签: javascript html reactjs redux