【问题标题】:firestoreConnect is not keeping the statefirestoreConnect 没有保持状态
【发布时间】:2020-11-22 14:50:14
【问题描述】:

我正在使用firestoreConnect,但数据似乎从未写入状态。

const Shipping = ({adoptions,cart,addresses,totals}) => {
  console.log("Shipping Addresses ",addresses)
  return <ViewCheckout to="/" cta="Next: Proceed Pay" totals={totals}  >
            <AddressBook />
            {/* Add address section */}
            <AddressForm />
    </ViewCheckout>
}
const mapPropsToState = state => { 
  
  console.log("state",state)
  return{
    cart: state.cart,
    totals: totalsSelector(state),
    firebase: state.firebase,
    addresses: state.firestore.data.addresses
  }
}


export default compose(
  firestoreConnect(['addresses']),
  connect(mapPropsToState,null))(Shipping)

我在redux-logger 中看到数据已加载,但是如果我读取状态,则数据永远不会加载。

我从状态添加日志:

  1. 当我调度 action:"@@reduxFirestore/DOCUMENT_ADDED" 时,该州拥有带有地址的 Firebase 数据。
  2. 但是。当我派出行动时:CREATE_ADDRESS_SUCCESS。 Firebase 数据不再加载,处于以前的状态。

我发现我的组件中从未出现过 firebase 数据,而且我从 reducer 中分派的任何下一个操作也是如此。

我可以看到,从 addAddress 函数记录状态也没有显示该状态的 Firebase 数据。

export function addAddress(data){

    return (dispatch,getState, {getFirebase,getFirestore}) => {


       const firesotre = getFirestore()
        
       console.log("Action state",getState())

        return firesotre.add({collection:'addresses'},
            {...data,
                createdAt: new Date() 
            })
        .then( ()=> dispatch(createAddressSuccess()) )
        .catch( ()=> dispatch(addressError()) )
    }
}

日志数据:

Redux Inspector 显示两个实例。

  1. 一个实例状态捕获我在项目中创建的所有操作。不使用 firebase 进行更新。
  2. 另一方面,状态的行为相反,只跟踪有关火力基地的状态。它不监听应用程序操作。

有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

    我自己解决了!

    我忘记了我使用了一个 gatsby 插件来创建商店。

    {
       resolve: `gatsby-plugin-react-redux`,
       options: {
         pathToCreateStoreModule: "./src/store",
          // [optional] - options passed to `serialize-javascript`
        //     // info: https://github.com/yahoo/serialize-javascript#options
        //     // will be merged with these defaults:
        //     serialize: {
        //       space: 0,
        //       // if `isJSON` is set to `false`, `eval` is used to deserialize redux state,
        //       // otherwise `JSON.parse` is used
        //       isJSON: true,
        //       unsafe: false,
        //       ignoreFunction: true,
        //     },
        //     // [optional] - if true will clean up after itself on the client, default:
        //     cleanupOnClient: true,
        //     // [optional] - name of key on `window` where serialized state will be stored, default:
        //     windowKey: "__PRELOADED_STATE__",
        //   },
        // },
    

    使用 Gatsby 时请记住检查配置文件中的内容!!!不然的话,好像代码要了自己的命!

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 1970-01-01
      • 2021-03-04
      • 2010-11-16
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 2012-03-09
      相关资源
      最近更新 更多