3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[React,TypeScript]無限import編が起きてしまった時の解決策

Last updated at Posted at 2022-04-19

はじめに

Reactのアプリを開発している時に以下の様なエラーを吐いてコンパイルできなくなってしまったので、その解決法を記しておきます

エラー内容

Dependency cycle detected ...

このエラーを調べてみるとimportし続けてしまう連鎖が起きているらしい

エラーの解説

1.chatDataSlice.tsからRootStateをimportする
2.reduxType.tsからstoreをimportする
3.store.tsからchatDataReducerをimportする

この一連の流れがループしていることでエラーが発生している

chatDataSlice.ts
/* packages */
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
/* hooks */
import { useAppSelector } from "../../hooks/useAppselector";
/* types */
import { chatData } from "../../types/chatData";
import { RootState } from "../types/reduxType";

const initialState: chatData[] = [];

export const chatDataSlice = createSlice({
  name: "chatData",
  initialState,
  reducers: {
    setChatData: (state, action: PayloadAction<chatData[]>) => action.payload,
  },
});

// reducer
export const chatDataReducer = chatDataSlice.reducer;

// action
export const { setChatData } = chatDataSlice.actions;

// selector
export const useChatDataSelector = () => {
  const chatDataSelector = useAppSelector((state: RootState) => state.chatData);
  return chatDataSelector;
};
reduxType.ts
/* store */
import { store } from "../store";

export type RootState = ReturnType<typeof store.getState>;

export type AppDispatch = typeof store.dispatch;
store.ts
/* packages */
import { configureStore } from "@reduxjs/toolkit";
/* slice */
import { chatDataReducer } from "./slices/chatDataSlice";

export const store = configureStore({
  reducer: {
    chatData: chatDataReducer,
  },
});

原因

importしたファイルはmoduleではないかとコンパイラ側は認識してしまっているから無限import編が始まっている

解決策

importの後ろにtypeを明記することでエラーは無くなった
明記することでmoduleではなくtypeだと認識し、依存関係から除外されているのでは??

chatDataSlice.ts
import type {RootState} from "../types/reduxType";

参考

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?