ReferenceError: Cannot access 'api' before initialization
See original GitHub issueI am having trouble catching the action generated from the injectEndpoints
from the workspace api in my userSlice using the extraReducers
.
My code structure is as follow.
baseApi src/redux/services/api
export const api = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth,
endpoints: (builder) => ({
}),
});
Workspace src/redux/services/workspace
export const workspace = api.injectEndpoints({
endpoints: (builder) => ({
getDocs: builder.query<any, void>({
query: () => `/workspace/6236f159662bebb0c2669430/docs`,
}),
}),
});
export const { useGetDocsQuery } = workspace;
User Slice src/redux/splices/user
const userSlice = createSlice({
name: "user",
initialState: { user: null } as UserState,
reducers: {
setUser: (state, action) => {
state.user = action.payload.user;
},
loggedOut: (state) => {
state.user = null;
},
},
extraReducers: (builder) => {
builder.addMatcher(
workspace.endpoints.getDocs.matchFulfilled,
(state, { payload }) => {
console.log(payload);
}
);
},
});
The error : ReferenceError: Cannot access ‘api1’ before initialization
This Line : export const workspace = api.injectEndpoints({
If I put the code from the workspace api directly under the baseApi, it works without any issues so I am not sure how to solve this problem.
Edit : It looks like a circular dependency but I can’t seem to figure out how to sole it if the code is in another file.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:7 (2 by maintainers)
Top Results From Across the Web
ReferenceError: Cannot access before initialization in JS
The "Cannot access before initialization" error occurs when a variable declared using let or const is accessed before it was initialized in the...
Read more >ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >node.js - ReferenceError: Cannot access 'fs' before initialization
The error means that you're trying to use the variable fs BEFORE its const fs = ... or let fs = ... definition...
Read more >referenceerror: cannot access 'main' before initialization
The “cannot access before initialization” reference error occurs in JavaScript when you try to access a variable before it is declared with let...
Read more >Cannot access 'XXX' before initialization - r/angular ... - Reddit
Uncaught ReferenceError: Cannot access 'XXX' before initialization. Hello I get this error message when I try to import a component into another ...
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
just the code like https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#automatic-re-authorization-by-extending-fetchbasequery
it works if remove
maybe works if not use
injectEndpoints
apiNo, i had solved this problem by subscribing in extraReducer