-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
allow for circular references by building reducer lazily on first reducer call #1686
Conversation
@@ -191,7 +191,8 @@ describe('createSlice', () => { | |||
.addCase('increment', (state) => state + 1) | |||
.addCase('increment', (state) => state + 1), | |||
}) | |||
).toThrowErrorMatchingInlineSnapshot( | |||
slice.reducer(undefined, { type: 'unrelated' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only downside to this I could find. As the builder is now called lazily on first reducer call, createSlice
will not immediately toggle these errors.
But they will be triggered immediately on adding this to a store, so I'm pretty sure that would be okay.
Tests are failing because apparently the integration branch tests are just currently failing. Tests for the PR run fine locally. |
size-limit report 📦
|
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 addfab6:
|
@phryneas Per #1687, the build failures appear to be due to the Yarn v3 seems to fix that issue, but the docs aren't building clean atm. As a workaround, just change the version string back to |
if (!name) { | ||
throw new Error('`name` is a required option for createSlice') | ||
} | ||
const initialState = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✋ Doesn't this interfere with the lazy init logic I just added to createReducer
? We should be able to just pass this through.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since createReducer
will be called later than it was before, I wanted to keep the freezing behaviour as soon as possible. It should not really interfere with it, the "work" is just being done twice.
f27d3f4
to
455809f
Compare
Grr. TS 3.9 does not have |
And it builds green. W00p w00p. |
🚢 🚢 |
Hi there. Is there a plan to add similar functionality for After refactoring a module in my project from As a very hacky workaround for |
@MatthewWid That shouldn't really be a concern in most cases. The circular dependency issue only shows up with Do you have an actual example of a |
@markerikson The original code is proprietary so I cannot post it exactly, unfortunately, but I have the taken time to create a repository with a reproduction of what I am talking about: https://github.com/MatthewWid/rtk-createreducer-circular-dependencies Running instructions are in See
This occurs with the If you then checkout the Ideally, the first form on the Thanks so much for your responsiveness and work on RTK! |
@MatthewWid yeah, the problem here is you've managed to create a real circular dependency based on your own folder structure:
and this is made worse by the fact that you are using This can be avoided entirely given the current file structure if you just have the reducers import directly from those separate action files instead, ie, We might be able to update |
@markerikson That's understandable. As I say it's structured from Bulletproof React (that I believe is loosely similar to the Redux docs feature-folder recommendations?):
Otherwise you're necessarily relying on the internal structure of one feature from another feature so they are not as decoupled as they could be. Regardless, you're right in that the circular dependency is created by myself here, though the fact that this update seems to fix this exact issue only for Thanks for considering, anyway! |
The Redux docs do say "use feature folders", but personally I've always disliked the idea of "re-export everything from an Modifying |
Tbh, that bulletproof react recommendation might make sense for externally exported stuff, but that would still not mean that within that module you should import it from the index. That's how you get circularities - the index should only be used from the outside. |
This would solve most long-standing problem of circular references between slices by calling the builder callback lazily when the reducer is called first instead of immediately on reducer creation.