prescottprue / react-redux-firebase

Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
https://react-redux-firebase.com
MIT License
2.55k stars 559 forks source link

Accessing firebase from redux sagas in v3 (firebase.ref is not a function) #977

Open alexlouden opened 4 years ago

alexlouden commented 4 years ago

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

I've just upgraded from react-redux-firebase v2 to v3, and my root saga is throwing an error:

TypeError: firebase.ref is not a function

I was using getFirebase before (as per the v2 saga demo), but now I'm passing the firebase instance straight to the rootSaga.

I think there's a timing issue in the saga - it runs firebase.ref before the database is initialised by the context provider?

Is there a way to block the root saga from running until firebase is initialised? (If that's what was happening before?)

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via codesandbox or similar.

I've made a repro: https://codesandbox.io/s/redux-saga-firebase-issue-repro-9rij4?file=/src/store.js

What is the expected behavior?

Which versions of dependencies, and which browser and OS are affected by this issue? Did this work in previous versions or setups?

Library versions:

react 16.12.0 react-dom 16.12.0 react-redux 7.2.0 react-redux-firebase 3.6.0 react-scripts 3.0.1 redux-firestore 0.13.0 redux-saga 1.1.3

alexlouden commented 4 years ago

I ended up waiting for the ref to exist:

function waitForRef(firebase) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      if (firebase.ref) {
        resolve()
      }
    }, 1)
  })
}
solveretur commented 3 years ago

same here - had to downgrade to v2 otherwise had error that database didn't exist yet