Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(firebaseConnect): react-router race condition #463

Open
garyforsterio opened this issue May 10, 2018 · 8 comments
Open

bug(firebaseConnect): react-router race condition #463

garyforsterio opened this issue May 10, 2018 · 8 comments
Labels
Milestone

Comments

@garyforsterio
Copy link

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

(If this is a usage question, please do not post it here—post it on gitter. If this is not a “feature” or a “bug”, or the phrase “How do I...?” applies, then it's probably a usage question.)

What is the current behavior?
When transitioning between two components using react-router, the new component is mounted before the existing component is unmounted. Consequently, firebaseConnect() creates a listener before the old one has been detached. This in most cases is fine, however in my specific case, both components are subscribing to the same firebase path (albeit with different query parameters) which doesn't work. The way that listeners are currently detached (see here), means that both the new and the old listeners are
detached.

This if statement does not catch this edge case as my query parameters are different.

I think this could be overcome by passing the listener to off(listener) as documented here

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.

  1. Mount a component using firebaseConnect.
  2. Mount a second component using firebaseConnect and referencing the same firebase path with different queries.
  3. Unmount the first component.

What is the expected behavior?
The second component continues to receive data from firebase after the first component has unmounted.

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

    "react": "^16.3.2",
    "react-redux-firebase": "^2.1.1",
    "react-router-dom": "^4.2.2",
@prescottprue
Copy link
Owner

@garyforsterio Thanks for reporting, this is an interesting case! I agree with the off(listener) pattern, and have been looking to move towards it inside of react-redux-firebase for a while -- now there is a solid reason.

Interested if storeAs would be helpful as part workaround in the meantime?

Totally open to a pull request if you have the time. If not I'll try to get to it as soon as I can.

@prescottprue prescottprue changed the title React-router race condition bug(firestoreConnect): react-router race condition May 10, 2018
@prescottprue prescottprue changed the title bug(firestoreConnect): react-router race condition bug(firebaseConnect): react-router race condition May 10, 2018
@cryser29
Copy link

I faced the same issue. It seems that we should replace componentWillMount() with componentDidMount() in both firebaseConnect and firestoreConnect methods. In that case unsetListeners will be called before setListeners as it should be.

Do we have some another reason why componentWillMount() can be really required here?

Meanwhile, as a temporary workaround I've used allowMultipleListeners: true but oneListenerPerPath: true helps to resolve the issue too.

@prescottprue
Copy link
Owner

@cryser29 Thanks for the input and ideas for work arounds!

I would be interested to see what is impacted by this change. Some folks may be depending on this order, so it may warrant v3.

@garyforsterio
Copy link
Author

I was going to have a look at implementing the off(listener) pattern this week and try and put together a pull request but to be honest I think @cryser29 has a good point, especially so considering componentWillMount is being renamed/depreciated. Not really sure about any knock-on effects of this would be though...

@StanislavMayorov
Copy link

@cryser29 thanks.

reactReduxFirebase reducer is used to set this setting.

  reactReduxFirebase(firebase, {
    allowMultipleListeners: true,
    ...
  }),

@yevgenypats
Copy link

yevgenypats commented May 26, 2018

Hey Guys, Any news with that? got the same issue + the workaround didn't help so I have to use the following workaround: storeAs: 'incident_' + props.match.params.id
instead of using just storeAs: 'incident'

@yevgenypats
Copy link

Any news with that one?

prescottprue added a commit that referenced this issue Aug 13, 2018
* feat(firebaseConnect): HOCs switched to componentDidMount in place of componentWillMount  - #463
* feat(deps): update hoist-non-react-statics to 3.0.1
* fix(deps): remove gitbook-cli from dev dependencies (not used)
@prescottprue
Copy link
Owner

prescottprue commented Aug 13, 2018

@yevgenypats Due to the potential impact, work on this will go into the next major version. I started working on it on the v3.0.0-alpha branch. There may be a bunch of other big changes, so more updates to come about timelines.

As mentioned above, anyone experiencing this should use a unique storeAs to get around it. For instance, if you are seeing this on a project page, using:

storeAs: `project_${props.params.projectId}`

@prescottprue prescottprue added this to the v3.0.* milestone Feb 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants