Fix: Redux Error #9 - Dispatch in reducer - #40
Open
sentry[bot] wants to merge 1 commit into
Open
Conversation
❌ Deploy Preview for studenthub-student failed. Why did it fail? →
|
Comment on lines
+13
to
+22
| listenerMiddleware.startListening({ | ||
| actionCreator: setCredentials, | ||
| effect: () => { | ||
| // Emit asynchronously so the reducer has fully completed before any | ||
| // subscriber can trigger further dispatches (avoids Redux Error #9). | ||
| setTimeout(() => { | ||
| userLogin$.next({}); | ||
| }, 0); | ||
| }, | ||
| }); |
Author
There was a problem hiding this comment.
Bug: Post-login side effects are not triggered when the application reloads with a pre-existing authentication token from localStorage, as no setCredentials action is dispatched.
Severity: MEDIUM
Suggested Fix
Modify the application's initialization sequence. After the store is created with preloadedState, check if an authentication token exists. If it does, manually dispatch an action or trigger the userLogin$ subject to ensure all necessary post-login side effects, like fetching the user profile, are executed.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: src/store/store.ts#L13-L22
Potential issue: On application load, if an authentication token is present in
`localStorage`, the state is rehydrated with the user already authenticated. However,
the listener middleware is configured to trigger post-login side effects only upon a
`setCredentials` action. Since no such action is dispatched during state rehydration,
critical side effects, such as fetching the user's profile via the `userLogin$` stream,
are not executed. This results in the application operating with potentially stale or
incomplete user data for any user who reloads the page while logged in.
Did we get this right? 👍 / 👎 to inform future reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves the Redux Error #9, which occurred due to a 'dispatch in reducer' violation.
Root Cause:
The
setCredentialsreducer insrc/store/slices/authSlice.tswas directly callinguserLogin$.next({}). This synchronous emission, when subscribed to insrc/init.ts, could lead tooneSignalActionBasedOnStatus()being called, which in turn might dispatchsetShowOneSignalPrompt()while the originalsetCredentialsreducer was still executing. Redux prohibits dispatching actions from within a reducer, leading to Error #9.Solution:
userLogin$.next({})fromsrc/store/slices/authSlice.ts: The reducer is now pure and only updates the state.listenerMiddlewareinsrc/store/store.ts: A new listener was added that specifically watches for thesetCredentialsaction. OncesetCredentialsis dispatched and the reducer has completed, the listener asynchronously callsuserLogin$.next({})usingsetTimeout(0). This ensures that any subsequent dispatches triggered byuserLogin$subscribers happen outside the reducer's execution cycle, preventing the Redux error.Fixes SH-STUDENT-APP-3