我一直在尝试从 firestore 检索我的数据收集。但这是问题所在
import React, { Component } from 'react'
import Notification from './Notification.js'
import ProjectList from '../projects/ProjectList'
import { connect } from 'react-redux'
import { compose } from 'redux'
import { firestoreConnect } from 'react-redux-firebase'
class Dashboard extends Component {
render() {
//console.log(this.props)
const { projects } = this.props
return(
<div className="dashboard container">
<div className="row">
<div className="col s12 m6">
<ProjectList projects={projects} />
</div>
<div className="col s12 m5 offset-m1">
<Notification />
</div>
</div>
</div>
)
}
}
export default compose(
firestoreConnect(['projects']),
connect((state) => ({
projects: state.firestore.ordered.projects
}))
)(Dashboard)
这是我的根减速器
import authReducer from './authReducer'
import projectReducer from './projectReducer'
import { combineReducers } from 'redux'
import { firestoreReducer } from 'redux-firestore'
const rootReducer = combineReducers({
auth: authReducer,
project: projectReducer,
firestore: firestoreReducer
})
export default rootReducer
我的项目减速器是这样的
const initState = {
projects: [
{id: '0', title: 'do some JavaScript', content: 'blah blah blah...'},
{id: '1', title: 'grab some vegitables', content: 'blah blah blah...'},
{id: '2', title: 'have a cup of coffee', content: 'blah blah blah...'}
]
}
const projectReducer = (state = initState, action) => {
switch (action.type) {
case 'CREATE_PROJECT':
console.log('project is', action.project)
return state
case 'CREATE_PROJECT_ERR':
console.log('create project error', action.err)
return state
default:
return state
}
}
export default projectReducer
行动
export const createProject = (project) => {
return (dispatch, getState , { getFirebase, getFirestore }) => {
// console.log('project: ', project)
// adding data to firestore
const firestore = getFirestore()
firestore.collection('projects').add({
...project,
authorFirstName: 'Herold',
authorSecondName: 'Finch',
authorId: 111,
createdAt: new Date()
}).then(() => {
dispatch({type:'CREATE_PROJECT', project })
}).catch((err) => {
dispatch({type:'CREATE_PROJECT_ERR', err })
})
}
}
当我从这个 projectReducer 中获取静态数据时,一切都很好。但是我无法理解在从数据库中检索数据时缺少的地方。我尝试降级 react-redux-firebase,react-redux 仍然无法正常工作。请帮我解决这个问题,否则我需要知道是否还有其他方法可以选择。提前致谢!