【问题标题】:Deleting an item from a collection in f, using React Redux使用 React Redux 从 f 中的集合中删除项目
【发布时间】:2019-11-05 09:59:09
【问题描述】:

我正在尝试通过引用所选项目的 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' });
      })
  }
}

【问题讨论】:

    标签: reactjs firebase react-redux google-cloud-firestore


    【解决方案1】:

    发生这种情况是因为您的操作 deleteProject 没有被 redux 的 dispatch 调用。

    如果您观察正确,在您的 handleClick 函数中,您将直接调用 deleteProject 函数函数操作。

    handleClick 函数应该像这样从 prop 调用 deleteProject 函数。

    你的 handleClick 函数应该是 -

    const handleClick = (e, id, deleteProject) => { // passing deleteProject function from prop
        e.preventDefault()
        deleteProject(id)
        console.log(id)
    }
    

    你的 HTML 应该是 -

    <button onClick={(e) => handleClick(e, props.id, props.deleteProject)}>Delete</button>
    

    【讨论】:

      【解决方案2】:

      您调用的是deleteProject 的导入版本,而不是mapDispatchToProps 版本。这是一个常见的问题。

      解决此问题(并防止其在未来发生)的一种方法是将您在 mapDispatchToProps 中的操作重命名为不同的名称:

      const matchDispatchToProps = (dispatch) => {
          return {
              dispatchDeleteProject: (e, id) => {
                  e.preventDefault()
                  dispatch(deleteProject(id))
              })
          }
      }
      

      然后你可以从你的 props 中解构它并调用它:

      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>
              )
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-17
        • 2012-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多