React-redux-firebase: feat(database): access store state within firebaseConnect

Created on 24 Sep 2017  路  6Comments  路  Source: prescottprue/react-redux-firebase

Do you want to request a feature or report a bug?
It would be useful (sorry if there is already a way to achieve this), if it was possible to dynamically set the rootRef based on a redux value. This way a sort of multi-tenant structure could be used.

{
  "organisations": {
     "org_1": {
         "members": { ..},
        "otherData": { .. },
     },
     "org_2": {
         "members": { ..},
        "otherData": { .. },
    }
}

What is the current behavior?
Now I explicitly have to connect to redux before a query or action, ex:

export default compose(
  connect(state => ({ organisationKey: getOrgKey(state) }),
  firebaseConnect(({ organisationKey }) => [`/organisations/${organisationKey}/members`]),
  connect(mapStateToProps),
)(Members)

Would love to hear what you think (thanks for a very useful library).

enhancement help wanted question

All 6 comments

@minusoneman Connecting to state as you did in your example has been the accepted pattern.

Often times in bigger applications I have loaded this key or path in a higher component and passed it down.

Idea for v2

Are you using v2? Maybe a way this could be simplfied is have the second argument of functions passed to firebaseConnect receive the whole store instance of the current internal firebase instance. So something like:

export default compose(
  firebaseConnect((props, store) => [
    `/organisations/${getOrgKey(store.getState())}/members`
  ]),
  connect(mapStateToProps),
)(Members)

Not sure of any performance implication of doing this, or if it even makes things "more simple". Just an idea that I figured I would throw out there.

Thanks for the response! I am using v2, so let me try your suggestion and see if I encounter any performance issues.

@minusoneman What I proposed for v2 does not actually work with current syntax, it was just a proposal for possible syntax.

That said, I have begun to look into doing this for v2. Will update this issue with any progress.

After a bit of research, it seems that it is common practice to pass the results of store.getState, and it is actually how react-redux does it in connect. It makes sense to provide a similar API to connect by making the second argument the result of store.getState().

With that in mind, I went ahead and me the syntax as follows:

export default compose(
  firebaseConnect((props, store) => [
    `/organisations/${getOrgKey(store.getState())}/members`
  ]),
  connect(mapStateToProps),
)(Members)

This will be a breaking change for those that were depending on the second argument (store.firebase), but that is now passed as the third argument. The v2 migration guide will include this info.

Going to leave this issue open until it is merged and the docs are updated.

v2.0.0-beta.14 was released with store as the second argument of firebaseConnect as mentioned above.

More info was added to the docs including the migration guide which has a section about state based queries.

@prescottprue great! The syntax look good - I'll give beta.14 a spin and report back if I find any issues.

Thanks,

Was this page helpful?
0 / 5 - 0 ratings