reactjs - 使用 React Redux 从 f 中的集合中删除项目
问题描述
我试图通过引用所选项目的 id 从 Firestore 的集合中删除一个项目。我通过 mapDispatchToProps 成功传递了 id 直到操作,但在尝试通过 delete() 在 Firestore 中删除时停止。我认为问题可能出在我在firestore中删除的方法中,因为它停在那里。谁能告诉我我的代码有什么问题?
import React from 'react'
import { connect } from 'react-redux'
import { firestoreConnect } from "react-redux-firebase";
import { compose } from 'redux'
import { Redirect } from 'react-router-dom'
import moment from 'moment'
import { deleteProject } from '../../store/actions/projectActions'
const handleClick = (e, prop) => {
e.preventDefault()
deleteProject(prop)
console.log(prop)
}
const ProjectDetails = (props) => {
const { auth, project } = props;
if (!auth.uid) return <Redirect to='/signin' />
if (project) {
return (
<div className="container section project-details">
<div className="card z-depth-0">
// content here
</div>
<button onClick={(e) => handleClick(e, props.id)}>Delete</button>
</div>
</div>
)
} else {
return (
<div className="container center">
<p>Loading...</p>
</div>
)
}
}
const mapStateToProps = (state, ownProps) => {
const id = ownProps.match.params.id;
const projects = state.firestore.data.projects;
const project = projects ? projects[id] : null
return {
project: project,
id: id,
auth: state.firebase.auth
}
}
const matchDispatchToProps = (dispatch) => {
return {
deleteProject: (id) => dispatch(deleteProject(id))
}
}
export default compose(
connect(mapStateToProps, matchDispatchToProps),
firestoreConnect([
{ collection: 'projects' }
])
)(ProjectDetails)
export const deleteProject = (id) => {
console.log("dispatch", id) \\ successfully shows "dispatch", id
return(dispatch, getState, {getFirestore}) => {
const firestore = getFirestore();
firestore.collection('projects').doc(id).delete()
.then(() => {
console.log('deleted') \\ does not show deleted here
dispatch({ type: 'DELETE_PROJECT_SUCCESS' });
}).catch(err => {
dispatch({ type: 'DELETE_PROJECT_ERROR' });
})
}
}
解决方案
您正在调用导入的版本deleteProject
而不是mapDispatchToProps
版本。这是一个常见的问题。
解决此问题的一种方法(并防止它在将来发生)是将您在 mapDispatchToProps 中的操作重命名为不同的名称:
const matchDispatchToProps = (dispatch) => {
return {
dispatchDeleteProject: (e, id) => {
e.preventDefault()
dispatch(deleteProject(id))
})
}
}
然后你可以从你的道具中解构它并调用它:
const ProjectDetails = (props) => {
const { auth, project, dispatchDeleteProject } = props;
if (!auth.uid) return <Redirect to='/signin' />
if (project) {
return (
<div className="container section project-details">
<div className="card z-depth-0">
// content here
</div>
<button onClick={e=>dispatchDeleteProject(e, props.id)}>Delete</button>
</div>
</div>
)
}
推荐阅读
- mysql - 如何为 Laravel 的 prettus/l5-repository 添加多个 searchJoin 条件
- git - 重写 Git 历史记录时保持提交哈希的肮脏技巧?
- reactjs - React Native - stickyHeaderIndices 不适用于 SectionList
- python - 如何提示用户输入函数的整数,并将其作为字符串消息返回?
- spartacus-storefront - Spartacus 后端 OCC 登录端点更改
- python - Altair:Mark_rule 具有分层刻面的图例,另一个图例复制了图例
- android - 无法检查我的数组内部的文本视图在 Kotlin 中是否可见
- android - RecyclerView list 限制在一个片段,没有限制另一个片段
- flutter - 在 null 上调用了“add”方法:Flutter、SQFlite
- python - 在具有可变列表大小的 datframes 中的列中使用爆炸列表的有效方法