数组映射反应firebase redux

时间:2019-10-17 17:57:20

标签: react-native react-redux

我是个新手,我使用react firebase redux,如何显示返回对象的问题数组,实现此目的的最佳方法是什么?我想像列表一样返回它们:

import { compose, withHandlers,withProps } from 'recompose'
import { firebaseConnect } from 'react-redux-firebase'
import { Alerts,spinnerWhileLoading } from '@components'
import { connect } from 'react-redux'




export default compose(
  firebaseConnect([
    { type: 'once', path: '/questions' }
  ]),


  withHandlers({
    logout: ({ firebase }) => () =>
      firebase.logout().catch(err => Alerts.danger(err.message))
  }),

  connect((state, props) => ({
    questions: state.firebase.data.questions, 

// questions return Object {
  "-LrP3EkfZ8fCgkNycJ2-": Object {
    "question": "De déféré déféré",
    "user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
  },
  "-LrP4P3fQDK6DnS1hxDt": Object {
    "question": "De déféré déféré",
    "user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
  },
  "-LrPNX18hQLL-OjHWC3R": Object {
    "question": "Dddededed",
    "user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
  },
}
      })),
  spinnerWhileLoading(['questions'])


)

到我的index.js:

let Questions = ({ navigation: { navigate }, logout,questions}) => (
  <View style={{flex: 1}}>
{console.log(questions)}
//question array
<li question ="" />
<li question ="" />
//question array
</View>
export default enhancer(Questions)

1 个答案:

答案 0 :(得分:0)

如果我理解您应该:

  1. 将每个问题(如问题一样)保存在集合中。每次打开都是一个文档。
  2. 在获取问题时,您必须返回包含每个数据和ID的对象数组,例如:
not
query.onSnapshot(querySnapshot => {
  let arrayToReturn = [];
  querySnapshot.forEach(doc => {
    const objectData = {
      id: doc.id,
      ...doc.data()
    };
  arrayToReturn.push(objectData);
  });
return arrayToReturn;

  1. 映射数组并为每个问题返回You will have array like this: [ { "id": "-LrP3EkfZ8fCgkNycJ2-" "question": "De déféré déféré", "user": "9jNkvzr0chgPi0SC6rXMlVWdOF12" }, ... ]

让我知道它是否对您有帮助:)