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

First LOCATION_CHANGE does not trigger saga #126

Open
steinarb opened this issue May 4, 2024 · 4 comments
Open

First LOCATION_CHANGE does not trigger saga #126

steinarb opened this issue May 4, 2024 · 4 comments

Comments

@steinarb
Copy link

steinarb commented May 4, 2024

I use a saga called locationSaga to listen to LOCATION_CHANGE and for each LOCATION_CHANGE, look at the path and decide what redux actions to fire off to start loading the data needed by that path.

This means that I can lazy-load the data needed for that path.

However this does not works for reloads.

The first redux action as seen in redux devtool is a LOCATION_CHANGE with path /ukelonn/users (the path reloaded on)
image

However that LOCATION_CHANGE does not trigger this saga

Navigating to a different page and navigating back, triggers the saga:
image

One difference I can see is that the first LOCATION_CHANGE, the one that doesn't trigger the saga, is a pop() while the one that triggers the saga is a push()?

Or is it a timing related issue?

Should I have a saga that waits a couple of seconds and then fire off a push() to the current location?

@steinarb
Copy link
Author

steinarb commented May 4, 2024

Versions from package.json:

        "@reduxjs/toolkit": "^2.2.3",
        "axios": "^1.6.8",
        "delay": "^6.0.0",
        "js-cookie": "^3.0.5",
        "qs": "^6.12.0",
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "react-redux": "^9.1.0",
        "react-router-dom": "^6.22.3",
        "redux-first-history": "^5.2.0",
        "redux-saga": "^1.3.0",

@steinarb
Copy link
Author

steinarb commented May 4, 2024

Turns out firing off a PUSH LOCATION_CHANGE to the current router location at the react app startup in index.js, was enough. steinarb/ukelonn@dacca76

The PUSH LOCATION_CHANGE was picked up by the locationChange saga, the propert path was detected and the necessary redux actions for data required by the current location were dispatched (on an app reload of the path).

But even though I now have a workaround, it is still interesting to me to understand why the pop LOCATION_CHANGE wasn't picked up by the saga?

@salvoravida
Copy link
Owner

I guess because your saga started after the first dispatch of the action, so the saga middleware was not yet loaded with your saga to propagate the action

@steinarb
Copy link
Author

That sounds right. The dispatch of a LOCATION_CHANGE (i.e. my workaround) is done after I have set up the saga(s).

Thanks for responding! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants