Skip to content

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

Closed as not planned

Description

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions