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

Using useFirebaseConnect with null #953

Closed sebadom closed 4 years ago

sebadom commented 4 years ago

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

The documentation says that useFirebaseConnect can be potentially used passing null as an argument, therefore NO sync is done. First issue: null is not supported when using typescript, undefined is. Second issue: using this approach makes the component to throw an error when unwatches the event

What is the current behavior?

Navigating away from the route containing a component with this piece:

    useFirebaseConnect(editMode ? `courses/${match.params.id}` : null);

    const course:any = useSelector((state: RootState) => {
        if (editMode) {
            return state.firebase.data.courses && state.firebase.data.courses[match.params.id];
        } else {
            return initialStateCourse;
        }
    });

Throws this: Uncaught TypeError: Cannot read property 'forEach' of undefined,

It is exploting at this code

export function unWatchEvents(firebase, dispatch, events) {
    events.forEach((event) => unWatchEvent(firebase, dispatch, event))
}

Here you can reproduce it. Go from HOME to TODOS, then click HOME again. see the error

prescottprue commented 4 years ago

useFirebaseConnect should recieve an array argument instead of a string or null, so you could do:

    useFirebaseConnect(editMode ? [`courses/${match.params.id}`] : []);
sebadom commented 4 years ago

Mmm I see... let me try that out. On the other hand I believe the documentation is wrong perhaps

image

It says it supports array, string, object, function or null if we don't want to sync. And the example is using a string also

Let me set things up using just an array and I will come back. Thanks!

sebadom commented 4 years ago

I think its working fine with empty array! It might have been me, but I was confused by the documentation Thanks!