Skip to content

Allow providing custom slice creators when calling createSlice - #4348

Open
EskiMojo14 wants to merge 26 commits into
entity-methods-creatorfrom
per-slice-creator
Open

EskiMojo14 wants to merge 26 commits into
entity-methods-creatorfrom
per-slice-creator

Conversation

@EskiMojo14

@EskiMojo14 EskiMojo14 commented Apr 15, 2024 •

Copy link
Copy Markdown
Collaborator
const loaderSlice = createSlice({
  name: "loader",
  initialState: {} as Partial<Record<string, true>>,
  creators: { loader: loaderCreator },
  reducers: (create) => ({
    addLoader: create.loader({}),
  }),
});

@codesandbox

codesandbox Bot commented Apr 15, 2024

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web Editor • VS Code • Insiders

Open Preview

@codesandbox-ci

codesandbox-ci Bot commented Apr 15, 2024 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c7c88f2:

Sandbox Source
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

@EskiMojo14 EskiMojo14 changed the title Experiment with allowing providing custom slice creators when calling createSlice Allow providing custom slice creators when calling createSlice Jun 12, 2024
@EskiMojo14
EskiMojo14 marked this pull request as ready for review June 12, 2024 09:46
@EskiMojo14
EskiMojo14 force-pushed the entity-methods-creator branch from ed623ac to 11b873c Compare September 3, 2024 09:33
Comment on lines +1133 to +1135
reducers: (create) => ({
addLoader: create.loader({}),
}),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you please explain the differences between this and the traditional builder pattern? Also, would you mind clarifying the purpose of the custom loadCreator?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

what traditional builder pattern are you referring to?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

My reference was to the action builder pattern in Redux Toolkit, where we use methods like addCase and addMatcher in createSlice to dynamically build actions and reducers. With this PR allowing custom slice creators, I wonder if there are potential issues when multiple creators are provided for the same slice. Could this lead to naming conflicts or unexpected behavior if two creators define overlapping functionality for the same action or state? I might be missing something though haha

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

extraReducers has a different builder pattern, because we don't need a return value from it. for reducers we need the returned object to infer slice actions and case reducers from, similar to how you're required to return build.query when building an RTKQ API.

There's handling for overlaps, this is the third PR in a set of three. See #3837 for the base logic.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ahh gotcha, thanks Ben!

@aryaemami59 aryaemami59 linked an issue Mar 18, 2026 that may be closed by this pull request
@aryaemami59 aryaemami59 added the RTK-Query Issues related to Redux-Toolkit-Query label Mar 18, 2026
@aryaemami59 aryaemami59 added this to the 3.0 milestone Mar 18, 2026
@aryaemami59 aryaemami59 added the Awaiting Response Issues waiting for a reply from the OP or another party label Mar 18, 2026
@EskiMojo14 EskiMojo14 removed the RTK-Query Issues related to Redux-Toolkit-Query label Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Awaiting Response Issues waiting for a reply from the OP or another party

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom reducer creators

3 participants