"Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization" error in RTK Query
See original GitHub issueWhat exactly am I doing wrong here?
As soon as I add builder.addMatcher to extraReducers, I start getting an error. Everything works fine before I add it. totally the problem is related to my adding extraReducers. But I didn’t understand what I did wrong.
extraReducers: (builder) => {
builder.addMatcher(
authenticationApi.endpoints.postLogin.matchFulfilled,
(state, { payload }) => {
state.isLogin = true;
state.token = payload.data;
}
)
},
Store
Slice
Api
Api call page
Error
Issue Analytics
- State:
- Created a year ago
- Comments:8
Top Results From Across the Web
Redux Cannot access '__WEBPACK_DEFAULT_EXPORT__ ...
A few minutes into the refactoring, I was encountered with Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization .
Read more >redux cannot access before initialization - You.com
I have made a React site using redux which is successfully working on one component. This is mapped to props and pulls data...
Read more >Redux-Toolkit & Solving “ReferenceError: Access lexical ...
Last week, I had a really annoying error in one of our ... ReferenceError: can't access lexical declaration 'loadPage' before initialization ...
Read more >Code Splitting - Redux Toolkit
Code Splitting. RTK Query makes it possible to trim down your initial bundle size by allowing you to inject additional endpoints after ...
Read more >Redux Essentials, Part 8: RTK Query Advanced Patterns
Cache Data Subscription Lifetimes. Let's try this out and see what happens. Open up your browser's DevTools, go to the Network tab, and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think the issue is you have a circular import problem. The API file is importing the
baseQuery
,baseQuery
imports the auth action, and the auth slice imports the API.You’re probably going to need to rearrange that logic to fix the circle.
I don’t have an immediate answer on how to best do that atm - ironically I’m in the middle of fixing a bunch of circular imports in my own app and that’s taking up all my brain space 😃
The problem
I’ve resolved the issue so I think it deserves a little context first because I struggled to google answer fast.
My app uses Network.ts (basically a static class - singleton) file. The app uses Network.fetch instead of system fetch. The reason is so I can pass access token with every request.
Now this Network class is basically a wrapper around fetch and if it sees 401 it wants to dispatch empty auth state so user is shown “Not authenticated” screen
The issue is that when you init a new project using https://github.com/reduxjs/cra-template-redux-typescript (which you are so kind to contribute @markerikson) it doesn’t have a scenario for when you want to update the store from outside React component.
When there is UI action (say user clicks a button) it triggers Redux async action which uses internally the Network.ts class. That causes the problem as I described above.
The solution:
Keep the authSlice.ts file but create new auth-actions.ts, auth-reducers.ts and auth-types.ts The files can look like this (rather focus on the concept) auth-types.ts
auth-actions.ts
auth-reducers.ts
The authSlice.ts file looks like a typical Slice with one change
The store.ts has a slight change
And finally the Network.ts can dispatch to store