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

extraReducers 'ReferenceError: Cannot access before initialization' with inject #4303

Open
zz707 opened this issue Mar 25, 2024 · 2 comments

Comments

@zz707
Copy link

zz707 commented Mar 25, 2024

store.ts

export interface LazyLoadedSlices {}

export const rootReducer = combineSlices(
  appSlice,
  currentUserSlice,
).withLazyLoadedSlices<LazyLoadedSlices>();

projectEditSlice.ts

export const projectEditSliceLazy = createSlice({
  name: 'projectEdit',
  initialState,
  reducers: {
    setProjectCommonInfo: (),
  },
  extraReducers: (builder) => {
    builder
      .addCase(projectGetById.fulfilled, (state) => {
        state.isLoading = false;
      })
      .addCase(projectUpdateCommonInfo.fulfilled, (state) => {
        state.isLoading = false;
      });
  },
});

export const { actions: projectEditActions } = projectEditSliceLazy;

export const reducerWithProjectEditSlice =
  rootReducer.inject(projectEditSliceLazy);

projectUpdateCommonInfo.ts

export const projectUpdateCommonInfo = createAsyncThunk<...>
  ('project/update', async (updateData, thunkApi) => {
  const { extra, dispatch, rejectWithValue } = thunkApi;

  try {
    const response = await extra.api.patch<...>();
    ...
    dispatch(projectEditActions.setProjectCommonInfo(response.data));
  } catch {}
})

Only the await dispatch(projectGetById(id)); is called on the edit page and i recieve error ReferenceError: Cannot access 'projectUpdateCommonInfo' before initialization.

If I make a slice ordinary, without lazy loading and add it directly to the rootReducer, then everything works without errors.

Please tell me where my mistake is.

@zz707 zz707 changed the title extraReducer 'ReferenceError: Cannot access before initialization' with inject extraReducers 'ReferenceError: Cannot access before initialization' with inject Mar 25, 2024
@EskiMojo14
Copy link
Collaborator

it sounds like you might have a circular dependency issue - could you share a repo?

@zz707
Copy link
Author

zz707 commented Mar 28, 2024

could you share a repo?

Sorry, no.
It confuses me that everything works without inject.

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

No branches or pull requests

2 participants