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

Modernizing Redux Logic with Redux Toolkit (Integrate Redux Toolkit and optimize store configuration) #5867

Merged

Conversation

Abishekcs
Copy link
Contributor

@Abishekcs Abishekcs commented Jun 30, 2024

# Redux Toolkit Integration

What this PR does

This PR updates Redux store configuration to use Redux Toolkit, simplifying setup and leveraging built-in optimizations.

Changes

  • Replace createStore with configureStore from Redux Toolkit
  • Remove explicit thunk middleware import (now included by default)
  • Simplify middleware configuration
  • Add conditional Redux DevTools enablement
  • Remove manual compose enhancers setup

Open questions and concerns

For now, I have simply replaced createStore with configureStore. This follows the suggestion from the Redux migration guide on the official Redux website, which states:

"You should always start by replacing the legacy createStore call with configureStore. This is a one-time step, and all of the existing reducers and middleware will continue to work as-is. configureStore includes development-mode checks for common mistakes like accidental mutations and non-serializable values, so having those in place will help identify any areas of the codebase where those mistakes are happening."

For more details, refer to the Redux documentation:
https://redux.js.org/usage/migrating-to-modern-redux#modernizing-redux-logic-with-redux-toolkit

a) Immutable and Serializable Checks

I have currently set immutableCheck and serializableCheck (provided by Redux Toolkit) to false. This decision was made because state mutations and non-serializable values were detected in our existing code, as shown in the screenshots below:

Screenshot from 2024-06-30 15-51-05
Screenshot from 2024-06-30 16-20-11
Screenshot from 2024-06-30 16-24-16

I chose this approach for the following reasons:

  1. It allows us to complete the migration to Redux Toolkit without immediately addressing these issues.
  2. With these checks disabled, there are no errors or warnings, and the dashboard functions as intended.
  3. The development-mode checks provided by Redux Toolkit are temporarily disabled to prevent disruption to our current workflow.

b) Addressing Mutation and Serialization Issues

An important question to consider is whether we should address these mutation and serialization issues now or in the future, especially given the large number of reducers and actions that need to be checked for state mutations.

For example, we can enable mutation checks when resolving or detecting these issues by setting enableMutationChecks to true. Then, pick an existing slice reducer and its associated actions, and ensure there is no state mutation (RTK will automatically detect it). Replace them with RTK's createSlice. Repeat this process for one reducer at a time. After resolving all the state mutations, we can then permanently re-enable these checks i.e (development-mode checks for common mistakes like accidental mutations and non-serializable values).

c) Redux DevTools in Production

I have also disabled Redux DevTools for production, but I'm not sure if this will be helpful.

Any suggestions regarding these three issues would be greatly appreciated. Thank you.

- Replace createStore with configureStore from Redux Toolkit
- Remove explicit thunk middleware (now included by default)
- Simplify middleware setup and add conditional checks
- Enable Redux DevTools only in non-production environments
- Remove manual compose enhancers configuration

This update improves maintainability and adopts Redux best practices.
@Abishekcs Abishekcs changed the title Integrate Redux Toolkit and optimize store configuration Modernizing Redux Logic with Redux Toolkit (Integrate Redux Toolkit and optimize store configuration) Jun 30, 2024
@Abishekcs Abishekcs marked this pull request as ready for review July 1, 2024 11:47
@@ -10654,6 +10691,13 @@ __metadata:
languageName: node
linkType: hard

"redux@npm:^5.0.1":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds redux 5 without also removing redux 4. Is that the expected behavior for this first stage, or should we expect to only use a single version of redux.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds redux 5 without also removing redux 4. Is that the expected behavior for this first stage, or should we expect to only use a single version of redux.

We need to keep both versions for now.
The expected behavior when adding Redux Toolkit is indeed to have Redux 5 added without immediately removing Redux 4. However, the next step in the migration process will be to remove the direct dependency on Redux 4 (and other packages like redux-thunk and reselect) from the package.json, as they're now provided by Redux Toolkit. This should be done only after we have fully migrated to Modern Redux.

Source of Information: https://redux.js.org/tutorials/fundamentals/part-8-modern-redux#package-cleanup

@ragesoss
Copy link
Member

ragesoss commented Jul 1, 2024

I am not familiar with Redux Toolkit, but it sounds like a good idea to use it and then find and fix the reducers that have state mutations.

@Abishekcs
Copy link
Contributor Author

Abishekcs commented Jul 2, 2024

I am not familiar with Redux Toolkit, but it sounds like a good idea to use it and then find and fix the reducers that have state mutations.

Great! 😁 I am still going through the official Redux docs and style guide for modern Redux. I will try to fix those two mutations as shown in the screenshot of this PR. I will also try to replace the old reducers and actions with Redux Toolkit's createSlice.

@ragesoss ragesoss merged commit 1bbb319 into WikiEducationFoundation:master Jul 8, 2024
1 check passed
@Abishekcs Abishekcs deleted the MigrateToModernRedux branch July 9, 2024 08:32
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

Successfully merging this pull request may close these issues.

2 participants